2 * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.lispflowmapping.mapcache;
11 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.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
22 * Flat key implementation of a map-cache. As the name suggests, no longest prefix matching is done for IP addresses
23 * or their derivatives.
25 * @author Florin Coras
29 public class FlatMapCache implements IMapCache {
32 public FlatMapCache(ILispDAO dao) {
37 public void addMapping(Eid eid, Object data, boolean shouldOverwrite, boolean shouldMerge) {
38 Eid key = MaskUtil.normalize(eid);
39 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
40 dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
44 public Object getMapping(Eid srcKey, Eid dstKey) {
48 Eid key = MaskUtil.normalize(dstKey);
49 return dao.getSpecific(key, SubKeys.RECORD);
53 public Eid getWidestNegativeMapping(Eid key) {
58 public void removeMapping(Eid eid, boolean overwrite) {
59 Eid key = MaskUtil.normalize(eid);
60 dao.removeSpecific(key, SubKeys.RECORD);
64 public void addAuthenticationKey(Eid eid, MappingAuthkey authKey) {
65 Eid key = MaskUtil.normalize(eid);
66 dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
70 public MappingAuthkey getAuthenticationKey(Eid eid) {
71 Eid key = MaskUtil.normalize(eid);
72 Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
73 if (data instanceof MappingAuthkey) {
74 return (MappingAuthkey) data;
81 public void removeAuthenticationKey(Eid eid) {
82 Eid key = MaskUtil.normalize(eid);
83 dao.removeSpecific(key, SubKeys.AUTH_KEY);
87 public void updateMappingRegistration(Eid eid, Long timestamp) {
88 Eid key = MaskUtil.normalize(eid);
89 if (dao.get(key) != null) {
90 if (timestamp == null) {
91 timestamp = System.currentTimeMillis();
93 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(timestamp)));
98 public void addData(Eid eid, String subKey, Object data) {
99 Eid key = MaskUtil.normalize(eid);
100 dao.put(key, new MappingEntry<>(subKey, data));
104 public Object getData(Eid eid, String subKey) {
105 Eid key = MaskUtil.normalize(eid);
106 return dao.getSpecific(key, subKey);
110 public void removeData(Eid eid, String subKey) {
111 Eid key = MaskUtil.normalize(eid);
112 dao.removeSpecific(key, subKey);
116 public String printMappings() {
117 final StringBuffer sb = new StringBuffer();
118 sb.append("Keys\tValues\n");
119 dao.getAll(new IRowVisitor() {
122 public void visitRow(Object keyId, String valueKey, Object value) {
123 String key = keyId.getClass().getSimpleName() + "#" + keyId;
124 if (!lastKey.equals(key)) {
125 sb.append("\n" + key + "\t");
127 sb.append(valueKey + "=" + value + "\t");
132 return sb.toString();