Reorganize mappingservice.implementation
[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.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.concurrent.TimeUnit;
15
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.RLOCGroup;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class HashMapDb implements ILispDAO, AutoCloseable {
24
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;
29
30     @Override
31     public void put(Object key, MappingEntry<?>... values) {
32         if (!data.containsKey(key)) {
33             data.put(key, new ConcurrentHashMap<String, Object>());
34         }
35         for (MappingEntry<?> entry : values) {
36             data.get(key).put(entry.getKey(), entry.getValue());
37         }
38     }
39
40     @Override
41     public Object getSpecific(Object key, String valueKey) {
42         Map<String, Object> keyToValues = data.get(key);
43         if (keyToValues == null) {
44             return null;
45         }
46         return keyToValues.get(valueKey);
47     }
48
49     @Override
50     public Map<String, Object> get(Object key) {
51         return data.get(key);
52     }
53
54     @Override
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());
59             }
60         }
61     }
62
63     @Override
64     public void remove(Object key) {
65         data.remove(key);
66     }
67
68     @Override
69     public void removeSpecific(Object key, String valueKey) {
70         if (data.containsKey(key) && data.get(key).containsKey(valueKey)) {
71             data.get(key).remove(valueKey);
72         }
73     }
74
75     @Override
76     public void removeAll() {
77         data.clear();
78     }
79
80     public void cleanOld() {
81         getAll(new IRowVisitor() {
82             public void visitRow(Object keyId, String valueKey, Object value) {
83                 if (value instanceof RLOCGroup) {
84                     RLOCGroup rloc = (RLOCGroup) value;
85                     if (isExpired(rloc)) {
86                         removeSpecific(keyId, valueKey);
87                     }
88                 }
89             }
90
91             private boolean isExpired(RLOCGroup rloc) {
92                 return System.currentTimeMillis() - rloc.getRegisterdDate().getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
93             }
94         });
95     }
96
97     public TimeUnit getTimeUnit() {
98         return timeUnit;
99     }
100
101     public void setRecordTimeOut(int recordTimeOut) {
102         this.recordTimeOut = recordTimeOut;
103     }
104
105     public int getRecordTimeOut() {
106         return recordTimeOut;
107     }
108
109     public void setTimeUnit(TimeUnit timeUnit) {
110         this.timeUnit = timeUnit;
111     }
112
113     public void close() throws Exception {
114         data.clear();
115     }
116
117 }