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.implementation.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.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.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 key, Object data, boolean shouldOverwrite) {
38 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
39 dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
43 public Object getMapping(Eid srcKey, Eid dstKey) {
47 return dao.getSpecific(dstKey, SubKeys.RECORD);
51 public void removeMapping(Eid key, boolean overwrite) {
52 dao.removeSpecific(key, SubKeys.RECORD);
56 public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
57 dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
61 public MappingAuthkey getAuthenticationKey(Eid key) {
62 Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
63 if (data instanceof MappingAuthkey) {
64 return (MappingAuthkey) data;
71 public void removeAuthenticationKey(Eid key) {
72 dao.removeSpecific(key, SubKeys.AUTH_KEY);
76 public void updateMappingRegistration(Eid key) {
77 if (dao.get(key) != null) {
78 dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
83 public void addData(Eid key, String subKey, Object data) {
84 dao.put(key, new MappingEntry<>(subKey, data));
88 public Object getData(Eid key, String subKey) {
89 return dao.getSpecific(key, subKey);
93 public void removeData(Eid key, String subKey) {
94 dao.removeSpecific(key, subKey);
98 public String printMappings() {
99 final StringBuffer sb = new StringBuffer();
100 sb.append("Keys\tValues\n");
101 dao.getAll(new IRowVisitor() {
104 public void visitRow(Object keyId, String valueKey, Object value) {
105 String key = keyId.getClass().getSimpleName() + "#" + keyId;
106 if (!lastKey.equals(key)) {
107 sb.append("\n" + key + "\t");
109 sb.append(valueKey + "=" + value + "\t");
114 return sb.toString();