2 * Copyright (c) 2014 Contextream, Inc. and others. 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
8 package org.opendaylight.lispflowmapping.mapcache;
10 import java.util.Date;
12 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
13 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
14 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
15 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
16 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
17 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
18 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Multi table map-cache that works with 'simple' and SourceDest LCAF addresses (see lisp-proto.yang). It can do longest
27 * prefix matching for IP and SourceDest LCAF addresses. In case of the latter, it uses two tables, one for dst and
28 * another for source, queried and populated in this exact order.
30 * @author Florin Coras
32 public class MultiTableMapCache implements IMapCache {
33 private static final Logger LOG = LoggerFactory.getLogger(MultiTableMapCache.class);
37 public MultiTableMapCache(ILispDAO dao) {
41 private ILispDAO getVniTable(Eid eid) {
43 if (eid.getVirtualNetworkId() == null) {
46 vni = eid.getVirtualNetworkId().getValue();
48 return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
51 private ILispDAO getOrInstantiateVniTable(Eid eid) {
53 if (eid.getVirtualNetworkId() == null) {
56 vni = eid.getVirtualNetworkId().getValue();
58 ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
60 table = dao.putNestedTable(vni, SubKeys.VNI);
65 public void addMapping(Eid key, Object value, boolean shouldOverwrite, boolean shouldMerge) {
66 Eid eid = MaskUtil.normalize(key);
67 ILispDAO table = getOrInstantiateVniTable(key);
69 if (eid.getAddress() instanceof SourceDestKey) {
70 Eid srcKey = SourceDestKeyHelper.getSrcBinary(eid);
71 ILispDAO srcDstDao = getOrInstantiateSDInnerDao(eid, table);
72 srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
73 srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.RECORD, value));
75 table.put(eid, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
76 table.put(eid, new MappingEntry<>(SubKeys.RECORD, value));
80 // Returns the mapping corresponding to the longest prefix match for eid.
81 // eid must be a simple (maskable or not) address
82 private Object getMappingLpmEid(Eid eid, ILispDAO mappingsDb) {
86 Map<String, ?> daoEntry = mappingsDb.getBest(MaskUtil.normalize(eid));
87 if (daoEntry != null) {
88 return daoEntry.get(SubKeys.RECORD);
94 // Returns a mapping corresponding to either the longest prefix match for both dstEid and srcEid,
95 // if a SourceDest mapping exists, or to dstEid
96 private Object getMappingLpmSD(Eid srcEid, Eid dstEid, ILispDAO mappingsDb) {
97 Map<String, ?> daoEntry = mappingsDb.getBest(MaskUtil.normalize(dstEid));
98 if (daoEntry != null) {
99 // try SrcDst eid lookup
100 ILispDAO srcDstDao = (ILispDAO) daoEntry.get(SubKeys.LCAF_SRCDST);
101 if (srcDstDao != null) {
102 Object mapping = getMappingLpmEid(srcEid, srcDstDao);
103 if (mapping != null) {
108 // if lookup fails, return whatever is found for dst eid
109 return daoEntry.get(SubKeys.RECORD);
114 public Object getMapping(Eid srcEid, Eid dstEid) {
115 if (dstEid == null) {
119 ILispDAO table = getVniTable(dstEid);
124 // a map-request for an actual SrcDst LCAF, ignore src eid
125 if (dstEid.getAddress() instanceof SourceDestKey) {
126 Eid srcAddr = SourceDestKeyHelper.getSrcBinary(dstEid);
127 Eid dstAddr = SourceDestKeyHelper.getDstBinary(dstEid);
128 return getMappingLpmSD(srcAddr, dstAddr, table);
131 // potential map-request for SrcDst LCAF from non SrcDst capable devices
132 return getMappingLpmSD(srcEid, dstEid, table);
136 public Eid getWidestNegativeMapping(Eid key) {
137 return dao.getWidestNegativePrefix(key);
140 public void removeMapping(Eid eid, boolean overwrite) {
141 Eid key = MaskUtil.normalize(eid);
142 ILispDAO table = getVniTable(key);
147 if (key.getAddress() instanceof SourceDestKey) {
148 ILispDAO db = getSDInnerDao(key, table);
150 db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key),
154 table.removeSpecific(key, SubKeys.RECORD);
158 public void addAuthenticationKey(Eid eid, MappingAuthkey authKey) {
159 Eid key = MaskUtil.normalize(eid);
160 ILispDAO table = getOrInstantiateVniTable(key);
162 if (key.getAddress() instanceof SourceDestKey) {
163 ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
164 srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
166 table.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
170 private MappingAuthkey getAuthKeyLpm(Eid prefix, ILispDAO db) {
171 short maskLength = MaskUtil.getMaskForAddress(prefix.getAddress());
172 while (maskLength >= 0) {
173 Eid key = MaskUtil.normalize(prefix, maskLength);
174 Object password = db.getSpecific(key, SubKeys.AUTH_KEY);
175 if (password != null && password instanceof MappingAuthkey) {
176 return (MappingAuthkey) password;
183 public MappingAuthkey getAuthenticationKey(Eid eid) {
184 ILispDAO table = getVniTable(eid);
189 if (MaskUtil.isMaskable(eid.getAddress())) {
190 if (eid.getAddress() instanceof SourceDestKey) {
191 // NOTE: this is an exact match, not a longest prefix match
192 ILispDAO srcDstDao = getSDInnerDao(eid, table);
193 if (srcDstDao != null) {
194 return getAuthKeyLpm(SourceDestKeyHelper.getSrcBinary(eid), srcDstDao);
198 return getAuthKeyLpm(eid, table);
201 Eid key = MaskUtil.normalize(eid);
202 Object password = table.getSpecific(key, SubKeys.AUTH_KEY);
203 if (password != null && password instanceof MappingAuthkey) {
204 return (MappingAuthkey) password;
206 LOG.warn("Failed to find password!");
212 public void removeAuthenticationKey(Eid eid) {
213 Eid key = MaskUtil.normalize(eid);
214 ILispDAO table = getVniTable(key);
219 if (key.getAddress() instanceof SourceDestKey) {
220 ILispDAO srcDstDao = getSDInnerDao(key, table);
221 if (srcDstDao != null) {
222 srcDstDao.removeSpecific(key, SubKeys.AUTH_KEY);
225 table.removeSpecific(key, SubKeys.AUTH_KEY);
229 // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
230 // This method returns the DAO associated to a dst or creates it if it doesn't exist.
231 private ILispDAO getOrInstantiateSDInnerDao(Eid address, ILispDAO mappingsDb) {
232 Eid dstKey = SourceDestKeyHelper.getDstBinary(address);
233 ILispDAO srcDstDao = (ILispDAO) mappingsDb.getSpecific(dstKey, SubKeys.LCAF_SRCDST);
234 if (srcDstDao == null) {
235 // inserts nested table for source
236 srcDstDao = mappingsDb.putNestedTable(dstKey, SubKeys.LCAF_SRCDST);
241 // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
242 // This method returns the DAO associated to dst or null if it doesn't exist.
243 private ILispDAO getSDInnerDao(Eid address, ILispDAO mappingsDb) {
244 return (ILispDAO) mappingsDb.getSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
247 public String printMappings() {
248 final StringBuffer sb = new StringBuffer();
249 sb.append("Keys\tValues\n");
250 final IRowVisitor innerVisitor = (new IRowVisitor() {
253 public void visitRow(Object keyId, String valueKey, Object value) {
254 String key = keyId.getClass().getSimpleName() + "#" + keyId;
255 if (!lastKey.equals(key)) {
256 sb.append("\n" + key + "\t");
258 sb.append(valueKey + "=" + value + "\t");
262 final IRowVisitor vniVisitor = (new IRowVisitor() {
265 public void visitRow(Object keyId, String valueKey, Object value) {
266 String key = keyId.getClass().getSimpleName() + "#" + keyId;
267 if (!lastKey.equals(key)) {
268 sb.append(key + "\t");
270 if ((valueKey.equals(SubKeys.LCAF_SRCDST))) {
271 sb.append(valueKey + "= { ");
272 ((ILispDAO)value).getAll(innerVisitor);
275 sb.append(valueKey + "=" + value + "\t");
280 dao.getAll(new IRowVisitor() {
283 public void visitRow(Object keyId, String valueKey, Object value) {
284 String key = keyId.getClass().getSimpleName() + "#" + keyId;
285 if (!lastKey.equals(key)) {
286 sb.append("\n" + key + "\t");
288 if (valueKey.equals(SubKeys.VNI)) {
289 sb.append(valueKey + "= { ");
290 ((ILispDAO)value).getAll(vniVisitor);
293 sb.append(valueKey + "=" + value + "\t");
299 return sb.toString();
303 public void updateMappingRegistration(Eid key, Long timestamp) {
308 public void addData(Eid eid, String subKey, Object data) {
309 Eid key = MaskUtil.normalize(eid);
310 ILispDAO table = getOrInstantiateVniTable(key);
312 if (key.getAddress() instanceof SourceDestKey) {
313 ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
314 srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<>(subKey, data));
316 table.put(key, new MappingEntry<>(subKey, data));
321 public Object getData(Eid eid, String subKey) {
322 Eid key = MaskUtil.normalize(eid);
323 ILispDAO table = getVniTable(key);
328 if (key.getAddress() instanceof SourceDestKey) {
329 ILispDAO srcDstDao = getSDInnerDao(key, table);
330 return srcDstDao.getSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
332 return table.getSpecific(key, subKey);
337 public void removeData(Eid eid, String subKey) {
338 Eid key = MaskUtil.normalize(eid);
339 ILispDAO table = getVniTable(key);
343 if (key.getAddress() instanceof SourceDestKey) {
344 ILispDAO db = getSDInnerDao(key, table);
346 db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
349 table.removeSpecific(key, subKey);