add documentation to the mapping service TELSDN-568: #close
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / dao / InMemoryDAO.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  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.implementation.dao;
10
11 import java.lang.reflect.ParameterizedType;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18
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;
27
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;
34
35     public InMemoryDAO() {
36         typeToKeysToValues = new HashMap<Class<?>, Map<Object, Map<String, Object>>>();
37         scheduler = Executors.newScheduledThreadPool(1);
38         scheduler.scheduleAtFixedRate(new Runnable() {
39
40             public void run() {
41                 cleanOld();
42             }
43         }, 0, cleanInterval, timeUnit);
44     }
45
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());
51                 }
52             }
53         }
54     }
55
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)) {
64                             it.remove();
65                         }
66                     }
67                 }
68             }
69
70             private boolean isExpired(MappingServiceRLOC rloc) {
71                 return System.currentTimeMillis() - rloc.getRegisterdDate().getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
72             }
73         });
74     }
75
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());
81         }
82         keysToValues.put(key, keyToValues);
83     }
84
85     private <K> Map<Object, Map<String, Object>> getTypeMap(K key) {
86         if (key == null) {
87             throw new IllegalArgumentException("Illegal null key.");
88         }
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.");
92         }
93         return keysToValues;
94     }
95
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) {
101             return null;
102         }
103         return (V) keyToValues.get(valueKey.getKey());
104     }
105
106     public <K> Object getSpecific(K key, String valueKey) {
107         return getSpecific(key, new MappingValueKey<Object>(valueKey));
108     }
109
110     public <K> Map<String, ?> get(K key) {
111         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
112         return keysToValues.get(key);
113     }
114
115     public <K> boolean remove(K key) {
116         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
117         return keysToValues.remove(key) != null;
118     }
119
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>>());
123     }
124
125     public void clearAll() {
126         typeToKeysToValues.clear();
127     }
128
129     public TimeUnit getTimeUnit() {
130         return TimeUnit.MINUTES;
131     }
132
133     public void setTimeUnit(int recordTimeOut) {
134         this.recordTimeOut = recordTimeOut;
135     }
136
137     public int getRecordTimeOut() {
138         return recordTimeOut;
139     }
140
141     public void setTimeUnit(TimeUnit timeUnit) {
142     }
143
144 }