2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.implementation;
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;
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;
78 * The Mapping System coordinates caching of md-sal stored mappings and if so configured enables longest prefix match
81 * @author Florin Coras
82 * @author Lorand Jakab
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;
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;
102 private ISouthBoundMappingTimeoutService sbMappingTimeoutService;
104 public MappingSystem(ILispDAO dao, boolean iterateMask, NotificationPublishService nps, boolean mappingMerge) {
106 this.notificationPublishService = nps;
107 this.mappingMerge = mappingMerge;
110 sbMappingTimeoutService = new TimeBucketMappingTimeoutService(ConfigIni.getInstance()
111 .getNumberOfBucketsInTimeBucketWheel(), ConfigIni.getInstance().getRegistrationValiditySb(),
115 public void setDataStoreBackEnd(DataStoreBackEnd dataStoreBackEnd) {
116 this.dsbe = dataStoreBackEnd;
120 public void setMappingMerge(boolean mappingMerge) {
121 this.mappingMerge = mappingMerge;
125 public void setIterateMask(boolean iterate) {
126 LOG.error("Non-longest prefix match lookups are not properly supported, variable is set to true");
129 public void initialize() {
130 restoreDaoFromDatastore();
133 private void buildMapCaches() {
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
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);
150 public void addMapping(MappingOrigin origin, Eid key, MappingData mappingData) {
152 sbMappingTimeoutService.removeExpiredMappings();
154 if (mappingData == null) {
155 LOG.warn("addMapping() called with null mapping, ignoring");
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");
165 if (xtrId != null && mappingMerge) {
166 if (mappingData.isMergeEnabled()) {
167 smc.addMapping(key, xtrId, mappingData);
168 handleMergedMapping(key);
171 clearPresentXtrIdMappings(key);
172 smc.addMapping(key, xtrId, mappingData);
175 addOrRefreshMappingInTimeoutService(key, mappingData);
178 tableMap.get(origin).addMapping(key, mappingData);
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);
186 @SuppressWarnings("unchecked")
187 private void clearPresentXtrIdMappings(Eid key) {
188 List<MappingData> allXtrMappingList = (List<MappingData>) (List<?>) smc.getAllXtrIdMappings(key);
190 if (((MappingData) smc.getMapping(key, (XtrId) null)).isMergeEnabled()) {
191 LOG.trace("Different xTRs have different merge configuration!");
194 for (MappingData mappingData : allXtrMappingList) {
195 removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
199 private void addOrRefreshMappingInTimeoutService(Eid key, MappingData mappingData) {
200 Integer oldBucketId = (Integer) smc.getData(key, SubKeys.TIME_BUCKET_ID);
201 Integer updatedBucketId;
203 if (oldBucketId != null) {
205 updatedBucketId = sbMappingTimeoutService.refreshMapping(key, mappingData, oldBucketId);
207 updatedBucketId = sbMappingTimeoutService.addMapping(key, mappingData);
210 smc.addData(key, SubKeys.TIME_BUCKET_ID, updatedBucketId);
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);
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));
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);
243 recordBuilder.setAction(LispMessage.NEGATIVE_MAPPING_ACTION);
244 //if (getAuthenticationKey(eid) != null) {
245 // recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
247 recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
249 return recordBuilder.build();
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.
256 public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
258 sbMappingTimeoutService.removeExpiredMappings();
260 if (timestamp == null) {
261 timestamp = System.currentTimeMillis();
263 MappingData mappingData = (MappingData) smc.getMapping(null, key);
264 if (mappingData != null) {
265 mappingData.setTimestamp(new Date(timestamp));
266 addOrRefreshMappingInTimeoutService(key, mappingData);
268 LOG.warn("Could not update timestamp for EID {}, no mapping found", LispAddressStringifier.getString(key));
270 if (mappingMerge && xtrId != null) {
271 MappingData xtrIdMappingData = (MappingData) smc.getMapping(key, xtrId);
272 if (xtrIdMappingData != null) {
273 xtrIdMappingData.setTimestamp(new Date(timestamp));
275 LOG.warn("Could not update timestamp for EID {} xTR-ID {}, no mapping found",
276 LispAddressStringifier.getString(key), LispAddressStringifier.getString(xtrId));
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>());
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!");
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) {
298 LOG.warn("Service Index should be 255 for simple IP RLOCs!");
301 } else if (rloc.getAddress() instanceof ExplicitLocatorPath) {
302 ExplicitLocatorPath elp = (ExplicitLocatorPath) rloc.getAddress();
303 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
305 if (index < 0 || index > hops.size()) {
306 LOG.warn("Service Index out of bounds!");
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());
316 LOG.warn("Nothing to do with ServicePath mapping record");
321 private MappingData handleMergedMapping(Eid key) {
322 List<MappingData> expiredMappingDataList = new ArrayList<>();
323 Set<IpAddressBinary> sourceRlocs = new HashSet<>();
325 MappingData mergedMappingData = MappingMergeUtil.mergeXtrIdMappings(smc.getAllXtrIdMappings(key),
326 expiredMappingDataList, sourceRlocs);
328 for (MappingData mappingData : expiredMappingDataList) {
329 removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
332 if (mergedMappingData != null) {
333 smc.addMapping(key, mergedMappingData, sourceRlocs);
334 dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key, mergedMappingData));
335 addOrRefreshMappingInTimeoutService(key, mergedMappingData);
337 removeSbMapping(key, mergedMappingData);
339 return mergedMappingData;
343 public MappingData getMapping(Eid src, Eid dst) {
344 // NOTE: Currently we have two lookup algorithms implemented, which are configurable
346 if (ConfigIni.getInstance().getLookupPolicy() == IMappingService.LookupPolicy.NB_AND_SB) {
347 return getMappingNbSbIntersection(src, dst);
349 return getMappingNbFirst(src, dst);
354 public MappingData getMapping(Eid dst) {
355 return getMapping((Eid) null, dst);
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
363 return getMapping(src, dst);
366 return getSbMappingWithExpiration(src, dst, xtrId);
370 public MappingData getMapping(MappingOrigin origin, Eid key) {
371 if (origin.equals(MappingOrigin.Southbound)) {
372 return getSbMappingWithExpiration(null, key, null);
374 return (MappingData) tableMap.get(origin).getMapping(null, key);
377 private MappingData getMappingNbFirst(Eid src, Eid dst) {
379 // Default lookup policy is northboundFirst
380 //lookupPolicy == NB_FIRST
382 MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
384 if (nbMappingData == null) {
385 return getSbMappingWithExpiration(src, dst, null);
387 if (dst.getAddress() instanceof ServicePath) {
388 return updateServicePathMappingRecord(nbMappingData, dst);
390 return nbMappingData;
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.
397 MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
398 if (nbMappingData == null) {
399 return nbMappingData;
401 // no intersection for Service Path mappings
402 if (dst.getAddress() instanceof ServicePath) {
403 return updateServicePathMappingRecord(nbMappingData, dst);
405 MappingData sbMappingData = getSbMappingWithExpiration(src, dst, null);
406 if (sbMappingData == null) {
407 return nbMappingData;
409 // both NB and SB mappings exist. Compute intersection of the mappings
410 return MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData);
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);
422 public MappingData handleSbExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
423 if (mappingMerge && mappingData.isMergeEnabled()) {
424 return handleMergedMapping(key);
428 removeSbXtrIdSpecificMapping(key, xtrId, mappingData);
430 removeSbMapping(key, mappingData);
435 private void removeSbXtrIdSpecificMapping(Eid key, XtrId xtrId, MappingData mappingData) {
436 smc.removeMapping(key, xtrId);
437 dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
440 private void removeSbMapping(Eid key, MappingData mappingData) {
441 if (mappingData != null && mappingData.getXtrId() != null) {
442 removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
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);
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);
461 public Eid getWidestNegativePrefix(Eid key) {
462 if (!MaskUtil.isMaskable(key.getAddress())) {
463 LOG.warn("Widest negative prefix only makes sense for maskable addresses!");
467 // We assume that ILispMapCache#getWidestNegativeMapping() returns null for positive mappings, and 0/0
469 Eid nbPrefix = pmc.getWidestNegativeMapping(key);
470 if (nbPrefix == null) {
471 LOG.trace("getWidestNegativePrefix NB: positive mapping, returning null");
474 if (LOG.isTraceEnabled()) {
475 LOG.trace("getWidestNegativePrefix NB: {}", LispAddressStringifier.getString(nbPrefix));
478 Eid sbPrefix = smc.getWidestNegativeMapping(key);
479 if (sbPrefix == null) {
480 LOG.trace("getWidestNegativePrefix SB: positive mapping, returning null");
483 if (LOG.isTraceEnabled()) {
484 LOG.trace("getWidestNegativePrefix SB: {}", LispAddressStringifier.getString(sbPrefix));
487 // since prefixes overlap, just return the more specific (larger mask)
488 if (LispAddressUtil.getIpPrefixMask(nbPrefix) < LispAddressUtil.getIpPrefixMask(sbPrefix)) {
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);
501 if (LOG.isDebugEnabled()) {
502 LOG.debug("Removing mapping for EID {} from {}",
503 LispAddressStringifier.getString(key), origin);
505 if (LOG.isTraceEnabled() && mapping != null) {
506 LOG.trace(mapping.getString());
509 MappingData notificationMapping = mapping;
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());
523 removeSubscribers(key);
525 if (origin == MappingOrigin.Southbound) {
526 removeFromSbTimeoutService(key);
529 if (origin == MappingOrigin.Southbound && mapping != null && mapping.isPositive().or(false)) {
530 mergeNegativePrefixes(key);
532 // mergeNegativePrefixes() above removes the mapping, so addNegativeMapping() will work correctly
533 tableMap.get(origin).removeMapping(key);
536 if (notificationMapping != null) {
537 notifyChange(notificationMapping, subscribers, dstSubscribers, MappingChange.Removed);
541 private void notifyChange(MappingData mapping, Set<Subscriber> subscribers, Set<Subscriber> dstSubscribers,
542 MappingChange mappingChange) {
543 MappingChanged notification = MSNotificationInputUtil.toMappingChanged(mapping, subscribers, dstSubscribers,
546 notificationPublishService.putNotification(notification);
547 } catch (InterruptedException e) {
548 LOG.warn("Notification publication interrupted!");
554 * Merges adjacent negative prefixes and notifies their subscribers.
556 private void mergeNegativePrefixes(Eid eid) {
557 LOG.debug("Merging negative prefixes starting from EID {}", LispAddressStringifier.getString(eid));
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<>();
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);
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);
580 previousNode = currentNode;
581 currentNode = smc.getVirtualParentSiblingPrefix(previousNode);
584 for (Eid key : mergedMappings.keySet()) {
585 removeSbMapping(key, mergedMappings.get(key));
587 smc.removeMapping(eid);
589 addNegativeMapping(getVirtualParent(previousNode));
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(),
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(),
604 return LispAddressUtil.asIpv6PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);
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);
618 if (LOG.isDebugEnabled()) {
619 LOG.debug("Adding new subscriber {} for EID {}", subscriber.getString(),
620 LispAddressStringifier.getString(subscribedEid));
622 subscribers.add(subscriber);
623 addSubscribers(subscribedEid, subscribers);
626 private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
627 LoggingUtil.logSubscribers(LOG, address, subscribers);
628 subscriberdb.put(address, subscribers);
632 public Set<Subscriber> getSubscribers(Eid address) {
633 if (LOG.isDebugEnabled()) {
634 LOG.debug("Retrieving subscribers for EID {}", LispAddressStringifier.getString(address));
637 Set<Subscriber> subscribers = subscriberdb.get(address);
638 LoggingUtil.logSubscribers(LOG, address, subscribers);
642 private void removeSubscribers(Eid address) {
643 if (LOG.isDebugEnabled()) {
644 LOG.debug("Removing subscribers for EID {}", LispAddressStringifier.getString(address));
646 subscriberdb.remove(address);
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);
657 public MappingAuthkey getAuthenticationKey(Eid key) {
658 if (LOG.isDebugEnabled()) {
659 LOG.debug("Retrieving authentication key for {}", LispAddressStringifier.getString(key));
661 return akdb.getAuthenticationKey(key);
665 public void removeAuthenticationKey(Eid key) {
666 if (LOG.isDebugEnabled()) {
667 LOG.debug("Removing authentication key for {}", LispAddressStringifier.getString(key));
669 akdb.removeAuthenticationKey(key);
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);
678 tableMap.get(origin).addData(key, subKey, data);
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);
686 return tableMap.get(origin).getData(key, subKey);
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);
694 tableMap.get(origin).removeData(key, subKey);
698 public Eid getParentPrefix(Eid key) {
699 return smc.getParentPrefix(key);
704 * Restore all mappings and keys from mdsal datastore.
706 private void restoreDaoFromDatastore() {
707 List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
708 List<Mapping> mappings = dsbe.getAllMappings(LogicalDatastoreType.CONFIGURATION);
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.
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");
723 mappings.addAll(dsbe.getAllMappings(LogicalDatastoreType.OPERATIONAL));
725 dsbe.removeLastUpdateTimestamp();
727 LOG.info("Restoring {} mappings and {} keys from datastore into DAO", mappings.size(), authKeys.size());
729 for (Mapping mapping : mappings) {
730 addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
731 new MappingData(mapping.getMappingRecord()));
734 for (AuthenticationKey authKey : authKeys) {
735 addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
739 public void destroy() {
740 LOG.info("Mapping System is being destroyed!");
741 dsbe.saveLastUpdateTimestamp();
745 public String printMappings() {
746 sbMappingTimeoutService.removeExpiredMappings();
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();
757 public String prettyPrintMappings() {
758 sbMappingTimeoutService.removeExpiredMappings();
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();
770 private static String prettyPrintSubscribers(Map<Eid, Set<Subscriber>> subscribers) {
771 final StringBuilder sb = new StringBuilder();
772 for (Eid eid: subscribers.keySet()) {
774 sb.append(LispAddressStringifier.getString(eid));
776 sb.append(LispMapCacheStringifier.prettyPrintSubscriberSet(subscribers.get(eid), 4));
779 return sb.toString();
783 public String printKeys() {
784 return akdb.printKeys();
788 public String prettyPrintKeys() {
789 return akdb.prettyPrintKeys();
792 public void cleanCaches() {
794 subscriberdb.clear();
798 public void cleanSBMappings() {
799 smc = new SimpleMapCache(sdao);
803 public void setIsMaster(boolean isMaster) {
804 this.isMaster = isMaster;
808 public boolean isMaster() {