Migrate implementation/neutron/southbound to IETF YANG model
[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.eid.container.Eid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.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 key, Object data, boolean shouldOverwrite) {
38         dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
39         dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
40     }
41
42     @Override
43     public Object getMapping(Eid srcKey, Eid dstKey) {
44         if (dstKey == null) {
45             return null;
46         }
47         return dao.getSpecific(dstKey, SubKeys.RECORD);
48     }
49
50     @Override
51     public void removeMapping(Eid key, boolean overwrite) {
52         dao.removeSpecific(key, SubKeys.RECORD);
53     }
54
55     @Override
56     public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
57         dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
58     }
59
60     @Override
61     public MappingAuthkey getAuthenticationKey(Eid key) {
62         Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
63         if (data instanceof MappingAuthkey) {
64             return (MappingAuthkey) data;
65         } else {
66             return null;
67         }
68     }
69
70     @Override
71     public void removeAuthenticationKey(Eid key) {
72         dao.removeSpecific(key, SubKeys.AUTH_KEY);
73     }
74
75     @Override
76     public void updateMappingRegistration(Eid key) {
77         if (dao.get(key) != null) {
78             dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
79         }
80     }
81
82     @Override
83     public void addData(Eid key, String subKey, Object data) {
84         dao.put(key, new MappingEntry<>(subKey, data));
85     }
86
87     @Override
88     public Object getData(Eid key, String subKey) {
89         return dao.getSpecific(key, subKey);
90     }
91
92     @Override
93     public void removeData(Eid key, String subKey) {
94         dao.removeSpecific(key, subKey);
95     }
96
97     @Override
98     public String printMappings() {
99         final StringBuffer sb = new StringBuffer();
100         sb.append("Keys\tValues\n");
101         dao.getAll(new IRowVisitor() {
102             String lastKey = "";
103
104             public void visitRow(Object keyId, String valueKey, Object value) {
105                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
106                 if (!lastKey.equals(key)) {
107                     sb.append("\n" + key + "\t");
108                 }
109                 sb.append(valueKey + "=" + value + "\t");
110                 lastKey = key;
111             }
112         });
113         sb.append("\n");
114         return sb.toString();
115     }
116 }