2 * Copyright (c) 2013 Contextream, 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.dao;
11 import java.lang.reflect.ParameterizedType;
12 import java.util.HashMap;
13 import java.util.Iterator;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
21 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
22 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
25 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
26 import org.opendaylight.lispflowmapping.interfaces.dao.MappingValueKey;
28 public class InMemoryDAO implements ILispDAO, IQueryAll {
29 private Map<Class<?>, Map<Object, Map<String, Object>>> typeToKeysToValues;
30 private TimeUnit timeUnit = TimeUnit.SECONDS;
31 private int recordTimeOut = 240;
32 private int cleanInterval = 10;
33 private ScheduledExecutorService scheduler;
35 public InMemoryDAO() {
36 typeToKeysToValues = new HashMap<Class<?>, Map<Object, Map<String, Object>>>();
37 scheduler = Executors.newScheduledThreadPool(1);
38 scheduler.scheduleAtFixedRate(new Runnable() {
43 }, 0, cleanInterval, timeUnit);
46 public void getAll(IRowVisitor visitor) {
47 for (Map.Entry<Class<?>, Map<Object, Map<String, Object>>> typeEntry : typeToKeysToValues.entrySet()) {
48 for (Map.Entry<Object, Map<String, Object>> keyEntry : typeEntry.getValue().entrySet()) {
49 for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
50 visitor.visitRow(typeEntry.getKey(), keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
56 public void cleanOld() {
57 getAll(new IRowVisitor() {
58 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
59 if (value instanceof MappingServiceValue) {
60 MappingServiceValue msv = (MappingServiceValue) value;
61 for (Iterator<MappingServiceRLOC> it = msv.getRlocs().iterator(); it.hasNext();) {
62 MappingServiceRLOC rloc = it.next();
63 if (isExpired(rloc)) {
70 private boolean isExpired(MappingServiceRLOC rloc) {
71 return System.currentTimeMillis() - rloc.getRegisterdDate().getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
76 public <K> void put(K key, MappingEntry<?>... values) {
77 Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
78 Map<String, Object> keyToValues = new HashMap<String, Object>();
79 for (MappingEntry<?> entry : values) {
80 keyToValues.put(entry.getKey(), entry.getValue());
82 keysToValues.put(key, keyToValues);
85 private <K> Map<Object, Map<String, Object>> getTypeMap(K key) {
87 throw new IllegalArgumentException("Illegal null key.");
89 Map<Object, Map<String, Object>> keysToValues = typeToKeysToValues.get(key.getClass());
90 if (keysToValues == null) {
91 throw new IllegalArgumentException("Unknown key type " + key.getClass() + ". must register with IConverter first.");
96 @SuppressWarnings("unchecked")
97 public <K, V> V getSpecific(K key, MappingValueKey<V> valueKey) {
98 Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
99 Map<String, Object> keyToValues = keysToValues.get(key);
100 if (keyToValues == null) {
103 return (V) keyToValues.get(valueKey.getKey());
106 public <K> Object getSpecific(K key, String valueKey) {
107 return getSpecific(key, new MappingValueKey<Object>(valueKey));
110 public <K> Map<String, ?> get(K key) {
111 Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
112 return keysToValues.get(key);
115 public <K> boolean remove(K key) {
116 Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
117 return keysToValues.remove(key) != null;
120 public <UserType, DbType> void register(Class<? extends ILispTypeConverter<UserType, DbType>> userType) {
121 Class<?> eidType = (Class<?>) ((ParameterizedType) userType.getGenericInterfaces()[0]).getActualTypeArguments()[0];
122 typeToKeysToValues.put(eidType, new HashMap<Object, Map<String, Object>>());
125 public void clearAll() {
126 typeToKeysToValues.clear();
129 public TimeUnit getTimeUnit() {
130 return TimeUnit.MINUTES;
133 public void setTimeUnit(int recordTimeOut) {
134 this.recordTimeOut = recordTimeOut;
137 public int getRecordTimeOut() {
138 return recordTimeOut;
141 public void setTimeUnit(TimeUnit timeUnit) {