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.dao;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class HashMapDb implements ILispDAO {
25 protected static final Logger LOG = LoggerFactory.getLogger(HashMapDb.class);
26 private ConcurrentMap<Object, ConcurrentMap<String, Object>> data = new ConcurrentHashMap<Object, ConcurrentMap<String, Object>>();
27 private TimeUnit timeUnit = TimeUnit.SECONDS;
28 private int recordTimeOut = 240;
31 public void put(Object key, MappingEntry<?>... values) {
32 if (!data.containsKey(key)) {
33 data.put(key, new ConcurrentHashMap<String, Object>());
35 for (MappingEntry<?> entry : values) {
36 data.get(key).put(entry.getKey(), entry.getValue());
41 public Object getSpecific(Object key, String valueKey) {
42 Map<String, Object> keyToValues = data.get(key);
43 if (keyToValues == null) {
46 return keyToValues.get(valueKey);
50 public Map<String, Object> get(Object key) {
55 public void getAll(IRowVisitor visitor) {
56 for (ConcurrentMap.Entry<Object, ConcurrentMap<String, Object>> keyEntry : data.entrySet()) {
57 for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
58 visitor.visitRow(keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
64 public void remove(Object key) {
69 public void removeSpecific(Object key, String valueKey) {
70 if (data.containsKey(key) && data.get(key).containsKey(valueKey)) {
71 data.get(key).remove(valueKey);
76 public void removeAll() {
80 public void cleanOld() {
81 getAll(new IRowVisitor() {
82 public void visitRow(Object keyId, String valueKey, Object value) {
83 if (value instanceof MappingServiceRLOCGroup) {
84 MappingServiceRLOCGroup rloc = (MappingServiceRLOCGroup) value;
85 if (isExpired(rloc)) {
86 removeSpecific(keyId, valueKey);
91 private boolean isExpired(MappingServiceRLOCGroup rloc) {
92 return System.currentTimeMillis() - rloc.getRegisterdDate().getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
97 public TimeUnit getTimeUnit() {
101 public void setRecordTimeOut(int recordTimeOut) {
102 this.recordTimeOut = recordTimeOut;
105 public int getRecordTimeOut() {
106 return recordTimeOut;
109 public void setTimeUnit(TimeUnit timeUnit) {
110 this.timeUnit = timeUnit;