Update DAO API
[lispflowmapping.git] / mappingservice / inmemorydb / src / main / java / org / opendaylight / lispflowmapping / inmemorydb / HashMapDb.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.inmemorydb;
10
11 import java.util.Date;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.TimeUnit;
16
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
18 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
20 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class HashMapDb implements ILispDAO, AutoCloseable {
25
26     protected static final Logger LOG = LoggerFactory.getLogger(HashMapDb.class);
27     private ConcurrentMap<Object, ConcurrentMap<String, Object>> data = new ConcurrentHashMap<Object, ConcurrentMap<String, Object>>();
28     private TimeUnit timeUnit = TimeUnit.SECONDS;
29     private int recordTimeOut = 240;
30
31     @Override
32     public void put(Object key, MappingEntry<?>... values) {
33         if (!data.containsKey(key)) {
34             data.put(key, new ConcurrentHashMap<String, Object>());
35         }
36         for (MappingEntry<?> entry : values) {
37             data.get(key).put(entry.getKey(), entry.getValue());
38         }
39     }
40
41     @Override
42     public Object getSpecific(Object key, String valueKey) {
43         Map<String, Object> keyToValues = data.get(key);
44         if (keyToValues == null) {
45             return null;
46         }
47         return keyToValues.get(valueKey);
48     }
49
50     @Override
51     public Map<String, Object> get(Object key) {
52         return data.get(key);
53     }
54
55     @Override
56     public void getAll(IRowVisitor visitor) {
57         for (ConcurrentMap.Entry<Object, ConcurrentMap<String, Object>> keyEntry : data.entrySet()) {
58             for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
59                 visitor.visitRow(keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
60             }
61         }
62     }
63
64     @Override
65     public void remove(Object key) {
66         data.remove(key);
67     }
68
69     @Override
70     public void removeSpecific(Object key, String valueKey) {
71         if (data.containsKey(key) && data.get(key).containsKey(valueKey)) {
72             data.get(key).remove(valueKey);
73         }
74     }
75
76     @Override
77     public void removeAll() {
78         data.clear();
79     }
80
81     // TODO: this should be moved outside of DAO implementation
82     public void cleanOld() {
83         getAll(new IRowVisitor() {
84             public void visitRow(Object keyId, String valueKey, Object value) {
85                 if (value != null && valueKey instanceof String && ((String) valueKey).equals(SubKeys.REGDATE)) {
86                     Date date = (Date) value;
87                     if (isExpired(date)) {
88                         removeSpecific(keyId, SubKeys.RECORD);
89                     }
90                 }
91             }
92
93             private boolean isExpired(Date date) {
94                 return System.currentTimeMillis() - date.getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
95             }
96         });
97     }
98
99     public TimeUnit getTimeUnit() {
100         return timeUnit;
101     }
102
103     public void setRecordTimeOut(int recordTimeOut) {
104         this.recordTimeOut = recordTimeOut;
105     }
106
107     public int getRecordTimeOut() {
108         return recordTimeOut;
109     }
110
111     public void setTimeUnit(TimeUnit timeUnit) {
112         this.timeUnit = timeUnit;
113     }
114
115     public void close() throws Exception {
116         data.clear();
117     }
118
119     @Override
120     public ILispDAO putNestedTable(Object key, String valueKey) {
121         ILispDAO nestedTable = (ILispDAO) getSpecific(key, valueKey);
122         if (nestedTable != null) {
123             LOG.warn("Trying to add nested table that already exists. Aborting!");
124             return nestedTable;
125         }
126         nestedTable = new HashMapDb();
127         put(key, new MappingEntry<>(valueKey, nestedTable));
128         return nestedTable;
129     }
130 }