2 * Copyright (c) 2015 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 java.util.ArrayList;
12 import java.util.Date;
13 import java.util.EnumMap;
14 import java.util.HashSet;
15 import java.util.List;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.lispflowmapping.config.ConfigIni;
19 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
20 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
21 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
22 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
23 import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
24 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
25 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
26 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
27 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
28 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
29 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
30 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
31 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
32 import org.opendaylight.lispflowmapping.mapcache.MultiTableMapCache;
33 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * The Mapping System coordinates caching of md-sal stored mappings and if so configured enables longest prefix match
59 * @author Florin Coras
62 public class MappingSystem implements IMappingSystem {
63 private static final Logger LOG = LoggerFactory.getLogger(MappingSystem.class);
64 private static final String AUTH_KEY_TABLE = "authentication";
65 private boolean notificationService;
66 private boolean mappingMerge;
68 private ILispDAO sdao;
69 private ILispMapCache smc;
70 private IMapCache pmc;
71 private IAuthKeyDb akdb;
72 private final EnumMap<MappingOrigin, IMapCache> tableMap = new EnumMap<>(MappingOrigin.class);
73 private DataStoreBackEnd dsbe;
74 private boolean isMaster = false;
76 public MappingSystem(ILispDAO dao, boolean iterateMask, boolean notifications, boolean mappingMerge) {
78 this.notificationService = notifications;
79 this.mappingMerge = mappingMerge;
83 public void setDataStoreBackEnd(DataStoreBackEnd dsbe) {
88 public void setMappingMerge(boolean mappingMerge) {
89 this.mappingMerge = mappingMerge;
93 public void setIterateMask(boolean iterate) {
94 LOG.error("Non-longest prefix match lookups are not properly supported, variable is set to true");
97 public void initialize() {
98 restoreDaoFromDatastore();
101 private void buildMapCaches() {
103 * There exists a direct relationship between MappingOrigins and the tables that are part of the MappingSystem.
104 * Therefore, if a new origin is added, probably a new table should be instantiated here as well. Here we
105 * instantiate a SimpleMapCache for southbound originated LISP mappings and a MultiTableMapCache for northbound
106 * originated mappings. Use of FlatMapCache would be possible when no longest prefix match is needed at all,
107 * but that option is no longer supported in the code, since it was never tested and may lead to unexpected
110 sdao = dao.putTable(MappingOrigin.Southbound.toString());
111 pmc = new MultiTableMapCache(dao.putTable(MappingOrigin.Northbound.toString()));
112 smc = new SimpleMapCache(sdao);
113 akdb = new AuthKeyDb(dao.putTable(AUTH_KEY_TABLE));
114 tableMap.put(MappingOrigin.Northbound, pmc);
115 tableMap.put(MappingOrigin.Southbound, smc);
118 public void addMapping(MappingOrigin origin, Eid key, MappingData mappingData) {
119 if (mappingData == null) {
120 LOG.warn("addMapping() called with null mapping, ignoring");
124 if (origin == MappingOrigin.Southbound) {
125 XtrId xtrId = mappingData.getXtrId();
126 if (xtrId == null && mappingMerge && mappingData.isMergeEnabled()) {
127 LOG.warn("addMapping() called will null xTR-ID in MappingRecord, while merge is set, ignoring");
130 if (xtrId != null && mappingMerge) {
131 smc.addMapping(key, xtrId, mappingData);
132 if (mappingData.isMergeEnabled()) {
139 tableMap.get(origin).addMapping(key, mappingData);
143 * Since this method is only called when there is a hit in the southbound Map-Register cache, and that cache is
144 * not used when merge is on, it's OK to ignore the effects of timestamp changes on merging for now.
146 public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
147 if (timestamp == null) {
148 timestamp = System.currentTimeMillis();
150 MappingData mappingData = (MappingData) smc.getMapping(null, key);
151 if (mappingData != null) {
152 mappingData.setTimestamp(new Date(timestamp));
154 LOG.warn("Could not update timestamp for EID {}, no mapping found", LispAddressStringifier.getString(key));
157 MappingData xtrIdMappingData = (MappingData) smc.getMapping(key, xtrId);
158 if (xtrIdMappingData != null) {
159 xtrIdMappingData.setTimestamp(new Date(timestamp));
161 LOG.warn("Could not update timestamp for EID {} xTR-ID {}, no mapping found",
162 LispAddressStringifier.getString(key), LispAddressStringifier.getString(xtrId));
167 private MappingData updateServicePathMappingRecord(MappingData mappingData, Eid eid) {
168 // keep properties of original record
169 MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mappingData.getRecord());
170 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
172 // there should only be one locator record
173 if (mappingData.getRecord().getLocatorRecord().size() != 1) {
174 LOG.warn("MappingRecord associated to ServicePath EID has more than one locator!");
178 LocatorRecord locatorRecord = mappingData.getRecord().getLocatorRecord().get(0);
179 long serviceIndex = ((ServicePath) eid.getAddress()).getServicePath().getServiceIndex();
180 int index = LispAddressUtil.STARTING_SERVICE_INDEX - (int) serviceIndex;
181 Rloc rloc = locatorRecord.getRloc();
182 if (rloc.getAddress() instanceof Ipv4 || rloc.getAddress() instanceof Ipv6) {
184 LOG.warn("Service Index should be 255 for simple IP RLOCs!");
187 } else if (rloc.getAddress() instanceof ExplicitLocatorPath) {
188 ExplicitLocatorPath elp = (ExplicitLocatorPath) rloc.getAddress();
189 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
191 if (index < 0 || index > hops.size()) {
192 LOG.warn("Service Index out of bounds!");
196 SimpleAddress nextHop = hops.get(index).getAddress();
197 LocatorRecordBuilder lrb = new LocatorRecordBuilder(locatorRecord);
198 lrb.setRloc(LispAddressUtil.toRloc(nextHop));
199 recordBuilder.getLocatorRecord().add(lrb.build());
200 return new MappingData(recordBuilder.build());
202 LOG.warn("Nothing to do with ServicePath mapping record");
207 private MappingData mergeMappings(Eid key) {
208 List<XtrId> expiredMappings = new ArrayList<>();
209 Set<IpAddressBinary> sourceRlocs = new HashSet<>();
210 MappingData mergedMappingData = MappingMergeUtil.mergeXtrIdMappings(smc.getAllXtrIdMappings(key),
211 expiredMappings, sourceRlocs);
212 smc.removeXtrIdMappings(key, expiredMappings);
213 for (XtrId xtrId : expiredMappings) {
214 dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(xtrId));
216 if (mergedMappingData != null) {
217 smc.addMapping(key, mergedMappingData, sourceRlocs);
218 dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key,
219 mergedMappingData.getRecord().getSiteId(), mergedMappingData));
221 smc.removeMapping(key);
222 dsbe.removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key));
224 return mergedMappingData;
228 public MappingData getMapping(Eid src, Eid dst) {
229 // NOTE: Currently we have two lookup algorithms implemented, which are configurable
231 if (ConfigIni.getInstance().getLookupPolicy() == IMappingService.LookupPolicy.NB_AND_SB) {
232 return getMappingNbSbIntersection(src, dst);
234 return getMappingNbFirst(src, dst);
239 public MappingData getMapping(Eid dst) {
240 return getMapping((Eid) null, dst);
244 public MappingData getMapping(Eid src, Eid dst, XtrId xtrId) {
245 // Note: If xtrId is null, we need to go through regular policy checking else Policy doesn't matter
248 return getMapping(src, dst);
251 return getSbMappingWithExpiration(src, dst, xtrId);
255 public MappingData getMapping(MappingOrigin origin, Eid key) {
256 if (origin.equals(MappingOrigin.Southbound)) {
257 return getSbMappingWithExpiration(null, key, null);
259 return (MappingData) tableMap.get(origin).getMapping(null, key);
262 private MappingData getMappingNbFirst(Eid src, Eid dst) {
264 // Default lookup policy is northboundFirst
265 //lookupPolicy == NB_FIRST
267 MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
269 if (nbMappingData == null) {
270 return getSbMappingWithExpiration(src, dst, null);
272 if (dst.getAddress() instanceof ServicePath) {
273 return updateServicePathMappingRecord(nbMappingData, dst);
275 return nbMappingData;
278 private MappingData getMappingNbSbIntersection(Eid src, Eid dst) {
279 //lookupPolicy == NB_AND_SB, we return intersection
280 //of NB and SB mappings, or NB mapping if intersection is empty.
282 MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
283 if (nbMappingData == null) {
284 return nbMappingData;
286 // no intersection for Service Path mappings
287 if (dst.getAddress() instanceof ServicePath) {
288 return updateServicePathMappingRecord(nbMappingData, dst);
290 MappingData sbMappingData = getSbMappingWithExpiration(src, dst, null);
291 if (sbMappingData == null) {
292 return nbMappingData;
294 // both NB and SB mappings exist. Compute intersection of the mappings
295 return MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData);
298 private MappingData getSbMappingWithExpiration(Eid src, Eid dst, XtrId xtrId) {
299 MappingData mappingData = (MappingData) smc.getMapping(dst, xtrId);
300 if (mappingData != null && MappingMergeUtil.mappingIsExpired(mappingData)) {
301 return removeExpiredMapping(dst, xtrId, mappingData);
308 * This private method either removes the main mapping ONLY, or the xTR-ID mapping ONLY, based on xtrId being
309 * null or non-null. Caller functions should take care of removing both when necessary.
311 private MappingData removeExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
312 if (mappingMerge && mappingData.isMergeEnabled()) {
313 return mergeMappings(key);
316 smc.removeMapping(key);
317 dsbe.removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, mappingData.getRecord().getEid(),
318 new SiteId(mappingData.getRecord().getSiteId()), mappingData));
320 smc.removeMapping(key, xtrId);
321 dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
327 public Eid getWidestNegativePrefix(Eid key) {
328 Eid nbPrefix = pmc.getWidestNegativeMapping(key);
329 if (nbPrefix == null) {
333 Eid sbPrefix = smc.getWidestNegativeMapping(key);
334 if (sbPrefix == null) {
338 // since prefixes overlap, just return the more specific (larger mask)
339 if (LispAddressUtil.getIpPrefixMask(nbPrefix) < LispAddressUtil.getIpPrefixMask(sbPrefix)) {
347 public void removeMapping(MappingOrigin origin, Eid key) {
348 tableMap.get(origin).removeMapping(key);
349 if (notificationService) {
355 public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
356 LOG.debug("Adding authentication key '{}' with key-ID {} for {}", authKey.getKeyString(), authKey.getKeyType(),
357 LispAddressStringifier.getString(key));
358 akdb.addAuthenticationKey(key, authKey);
362 public MappingAuthkey getAuthenticationKey(Eid key) {
363 if (LOG.isDebugEnabled()) {
364 LOG.debug("Retrieving authentication key for {}", LispAddressStringifier.getString(key));
366 return akdb.getAuthenticationKey(key);
370 public void removeAuthenticationKey(Eid key) {
371 if (LOG.isDebugEnabled()) {
372 LOG.debug("Removing authentication key for {}", LispAddressStringifier.getString(key));
374 akdb.removeAuthenticationKey(key);
378 public void addData(MappingOrigin origin, Eid key, String subKey, Object data) {
379 if (LOG.isDebugEnabled()) {
380 LOG.debug("Add data of class {} for key {} and subkey {}", data.getClass(),
381 LispAddressStringifier.getString(key), subKey);
383 tableMap.get(origin).addData(key, subKey, data);
387 public Object getData(MappingOrigin origin, Eid key, String subKey) {
388 if (LOG.isDebugEnabled()) {
389 LOG.debug("Retrieving data for key {} and subkey {}", LispAddressStringifier.getString(key), subKey);
391 return tableMap.get(origin).getData(key, subKey);
395 public void removeData(MappingOrigin origin, Eid key, String subKey) {
396 if (LOG.isDebugEnabled()) {
397 LOG.debug("Removing data for key {} and subkey {}", LispAddressStringifier.getString(key), subKey);
399 tableMap.get(origin).removeData(key, subKey);
404 * Restore all mappings and keys from mdsal datastore.
406 private void restoreDaoFromDatastore() {
407 List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
408 List<Mapping> mappings = dsbe.getAllMappings(LogicalDatastoreType.CONFIGURATION);
411 * XXX By default, the operational datastore is not persisted to disk, either at run-time, or on shutdown,
412 * so the following will have no effect (getLastUpdateTimestamp() will fail, since it's reading from
413 * the operational datastore, and even if it didn't getAllMappings() will fail anyway). According to rovarga it
414 * should be possible to turn on persistence for the operational datastore editing
415 * etc/opendaylight/karaf/05-clustering.xml, by setting <persistence>true</persistence>. At the time of writing
416 * the below code block that didn't seem to work though.
418 Long lastUpdateTimestamp = dsbe.getLastUpdateTimestamp();
419 if (lastUpdateTimestamp != null && System.currentTimeMillis() - lastUpdateTimestamp
420 > ConfigIni.getInstance().getRegistrationValiditySb()) {
421 LOG.warn("Restore threshold passed, not restoring operational datastore into DAO");
423 mappings.addAll(dsbe.getAllMappings(LogicalDatastoreType.OPERATIONAL));
425 dsbe.removeLastUpdateTimestamp();
427 LOG.info("Restoring {} mappings and {} keys from datastore into DAO", mappings.size(), authKeys.size());
429 for (Mapping mapping : mappings) {
430 addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
431 new MappingData(mapping.getMappingRecord()));
434 for (AuthenticationKey authKey : authKeys) {
435 addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
439 public void destroy() {
440 LOG.info("Mapping System is being destroyed!");
441 dsbe.saveLastUpdateTimestamp();
445 public String printMappings() {
446 final StringBuffer sb = new StringBuffer();
447 sb.append("PolicyMapCache\n--------------\n");
448 sb.append(pmc.printMappings());
449 sb.append("SbMapCache\n----------\n");
450 sb.append(smc.printMappings());
451 return sb.toString();
455 public String printKeys() {
456 return akdb.printKeys();
459 public void cleanCaches() {
465 * XXX Mappings and keys should be separated for this to work properly, as is it will remove northbound originated
466 * authentication keys too, since they are currently stored in smc.
468 public void cleanSBMappings() {
469 smc = new SimpleMapCache(sdao);
473 public void setIsMaster(boolean isMaster) {
474 this.isMaster = isMaster;
478 public boolean isMaster() {