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