8148b4440b77f54a307f9f5aab15cd30cbdab1e4
[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.Map;
14
15 import org.opendaylight.lispflowmapping.dao.ILispDAO;
16 import org.opendaylight.lispflowmapping.dao.ILispTypeConverter;
17 import org.opendaylight.lispflowmapping.dao.IQueryAll;
18 import org.opendaylight.lispflowmapping.dao.IRowVisitor;
19
20 public class InMemoryDAO implements ILispDAO, IQueryAll {
21     private Map<Class<?>, Map<Object, Map<String, Object>>> typeToKeysToValues;
22
23     public InMemoryDAO() {
24         typeToKeysToValues = new HashMap<Class<?>, Map<Object, Map<String, Object>>>();
25     }
26
27     public void getAll(IRowVisitor visitor) {
28         for (Map.Entry<Class<?>, Map<Object, Map<String, Object>>> typeEntry : typeToKeysToValues.entrySet()) {
29             for (Map.Entry<Object, Map<String, Object>> keyEntry : typeEntry.getValue().entrySet()) {
30                 for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
31                     visitor.visitRow(typeEntry.getKey(), keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
32                 }
33             }
34         }
35     }
36
37     public <K> void put(K key, MappingEntry<?>... values) {
38         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
39         Map<String, Object> keyToValues = new HashMap<String, Object>();
40         for (MappingEntry<?> entry : values) {
41             keyToValues.put(entry.getKey(), entry.getValue());
42         }
43         keysToValues.put(key, keyToValues);
44     }
45
46     private <K> Map<Object, Map<String, Object>> getTypeMap(K key) {
47         if (key == null) {
48             throw new IllegalArgumentException("Illegal null key.");
49         }
50         Map<Object, Map<String, Object>> keysToValues = typeToKeysToValues.get(key.getClass());
51         if (keysToValues == null) {
52             throw new IllegalArgumentException("Unknown key type " + key.getClass() + ". must register with IConverter first.");
53         }
54         return keysToValues;
55     }
56
57     @SuppressWarnings("unchecked")
58     public <K, V> V getSpecific(K key, MappingValueKey<V> valueKey) {
59         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
60         Map<String, Object> keyToValues = keysToValues.get(key);
61         if (keyToValues == null) {
62             return null;
63         }
64         return (V) keyToValues.get(valueKey.getKey());
65     }
66
67     public <K> Object getSpecific(K key, String valueKey) {
68         return getSpecific(key, new MappingValueKey<Object>(valueKey));
69     }
70
71     public <K> Map<String, ?> get(K key) {
72         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
73         return keysToValues.get(key);
74     }
75
76     public <K> boolean remove(K key) {
77         Map<Object, Map<String, Object>> keysToValues = getTypeMap(key);
78         return keysToValues.remove(key) != null;
79     }
80
81     public <UserType, DbType> void register(Class<? extends ILispTypeConverter<UserType, DbType>> userType) {
82         Class<?> eidType = (Class<?>) ((ParameterizedType) userType.getGenericInterfaces()[0]).getActualTypeArguments()[0];
83         typeToKeysToValues.put(eidType, new HashMap<Object, Map<String, Object>>());
84     }
85
86     public void clearAll() {
87         typeToKeysToValues.clear();
88     }
89 }