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