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;
13 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
14 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
15 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
16 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
17 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
18 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
23 * Flat key implementation of a map-cache. As the name suggests, no longest prefix matching is done for IP addresses
24 * or their derivatives.
26 * @author Florin Coras
30 public class FlatMapCache implements IMapCache {
33 public FlatMapCache(ILispDAO dao) {
38 public void addMapping(Eid eid, Object data, boolean shouldOverwrite, boolean shouldMerge) {
39 Eid key = MaskUtil.normalize(eid);
40 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
41 dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
45 public Object getMapping(Eid srcKey, Eid dstKey) {
49 Eid key = MaskUtil.normalize(dstKey);
50 return dao.getSpecific(key, SubKeys.RECORD);
54 public Eid getWidestNegativeMapping(Eid key) {
59 public void removeMapping(Eid eid, boolean overwrite) {
60 Eid key = MaskUtil.normalize(eid);
61 dao.removeSpecific(key, SubKeys.RECORD);
65 public void addAuthenticationKey(Eid eid, MappingAuthkey authKey) {
66 Eid key = MaskUtil.normalize(eid);
67 dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
71 public MappingAuthkey getAuthenticationKey(Eid eid) {
72 Eid key = MaskUtil.normalize(eid);
73 Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
74 if (data instanceof MappingAuthkey) {
75 return (MappingAuthkey) data;
82 public void removeAuthenticationKey(Eid eid) {
83 Eid key = MaskUtil.normalize(eid);
84 dao.removeSpecific(key, SubKeys.AUTH_KEY);
88 public void updateMappingRegistration(Eid eid, Long timestamp) {
89 Eid key = MaskUtil.normalize(eid);
90 if (dao.get(key) != null) {
91 if (timestamp == null) {
92 timestamp = System.currentTimeMillis();
94 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(timestamp)));
99 public void addData(Eid eid, String subKey, Object data) {
100 Eid key = MaskUtil.normalize(eid);
101 dao.put(key, new MappingEntry<>(subKey, data));
105 public Object getData(Eid eid, String subKey) {
106 Eid key = MaskUtil.normalize(eid);
107 return dao.getSpecific(key, subKey);
111 public void removeData(Eid eid, String subKey) {
112 Eid key = MaskUtil.normalize(eid);
113 dao.removeSpecific(key, subKey);
117 public String printMappings() {
118 final StringBuffer sb = new StringBuffer();
119 sb.append("Keys\tValues\n");
120 dao.getAll(new IRowVisitor() {
123 public void visitRow(Object keyId, String valueKey, Object value) {
124 String key = keyId.getClass().getSimpleName() + "#" + keyId;
125 if (!lastKey.equals(key)) {
126 sb.append("\n" + key + "\t");
128 sb.append(valueKey + "=" + value + "\t");
133 return sb.toString();