b9281497f5e4561a53f58ede7b25a046fc728b80
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / mapcache / FlatMapCache.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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.mapcache;
10
11 import java.util.Date;
12
13 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
14 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
15 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
16 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
17 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
19
20 /**
21  * Flat key implementation of a map-cache. As the name suggests, no longest prefix matching is done for IP addresses
22  * or their derivatives.
23  *
24  * @author Florin Coras
25  *
26  */
27
28 public class FlatMapCache implements IMapCache {
29     private ILispDAO dao;
30
31     public FlatMapCache(ILispDAO dao) {
32         this.dao = dao;
33     }
34
35     @Override
36     public void addMapping(LispAddressContainer key, Object data, boolean shouldOverwrite) {
37         dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
38         dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
39     }
40
41     @Override
42     public Object getMapping(LispAddressContainer srcKey, LispAddressContainer dstKey) {
43         if (dstKey == null) {
44             return null;
45         }
46         return dao.getSpecific(dstKey, SubKeys.RECORD);
47     }
48
49     @Override
50     public void removeMapping(LispAddressContainer key, boolean overwrite) {
51         dao.removeSpecific(key, SubKeys.RECORD);
52     }
53
54     @Override
55     public void addAuthenticationKey(LispAddressContainer key, String authKey) {
56         dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
57     }
58
59     @Override
60     public String getAuthenticationKey(LispAddressContainer key) {
61         Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
62         if (data instanceof String) {
63             return (String) data;
64         } else {
65             return null;
66         }
67     }
68
69     @Override
70     public void removeAuthenticationKey(LispAddressContainer key) {
71         dao.removeSpecific(key, SubKeys.AUTH_KEY);
72     }
73
74     @Override
75     public void updateMappingRegistration(LispAddressContainer key) {
76         if (dao.get(key) != null) {
77             dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
78         }
79     }
80
81     @Override
82     public void addData(LispAddressContainer key, String subKey, Object data) {
83         dao.put(key, new MappingEntry<>(subKey, data));
84     }
85
86     @Override
87     public Object getData(LispAddressContainer key, String subKey) {
88         return dao.getSpecific(key, subKey);
89     }
90
91     @Override
92     public void removeData(LispAddressContainer key, String subKey) {
93         dao.removeSpecific(key, subKey);
94     }
95
96     @Override
97     public String printMappings() {
98         final StringBuffer sb = new StringBuffer();
99         sb.append("Keys\tValues\n");
100         dao.getAll(new IRowVisitor() {
101             String lastKey = "";
102
103             public void visitRow(Object keyId, String valueKey, Object value) {
104                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
105                 if (!lastKey.equals(key)) {
106                     sb.append("\n" + key + "\t");
107                 }
108                 sb.append(valueKey + "=" + value + "\t");
109                 lastKey = key;
110             }
111         });
112         sb.append("\n");
113         return sb.toString();
114     }
115 }