Merge "Time Bucket DS for handeling SB mapping timeout"
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / SimpleMapCache.java
1 /*
2  * Copyright (c) 2015, 2016 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.AbstractMap.SimpleImmutableEntry;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
19 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
20 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
21 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25
26 /**
27  * Simple map-cache that works with 'simple' addresses (see lisp-proto.yang). It can do longest prefix matching for IP
28  * addresses.
29  *
30  * @author Florin Coras
31  * @author Lorand Jakab
32  *
33  */
34 public class SimpleMapCache implements ILispMapCache {
35     private ILispDAO dao;
36
37     public SimpleMapCache(ILispDAO dao) {
38         this.dao = dao;
39     }
40
41     private ILispDAO getVniTable(Eid eid) {
42         long vni = 0;
43         if (eid.getVirtualNetworkId() == null) {
44             vni = 0;
45         } else {
46             vni = eid.getVirtualNetworkId().getValue();
47         }
48         return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
49     }
50
51     private ILispDAO getOrInstantiateVniTable(Eid eid) {
52         long vni = 0;
53         if (eid.getVirtualNetworkId() == null) {
54             vni = 0;
55         } else {
56             vni = eid.getVirtualNetworkId().getValue();
57         }
58         ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
59         if (table == null) {
60             table = dao.putNestedTable(vni, SubKeys.VNI);
61         }
62         return table;
63     }
64
65     private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
66         ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
67         if (table == null) {
68             table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
69         }
70         return table;
71     }
72
73     @Override
74     public void addMapping(Eid key, Object value) {
75         addMapping(key, value, null);
76     }
77
78     @Override
79     public void addMapping(Eid key, Object value, Set<IpAddressBinary> sourceRlocs) {
80         Eid eid = MaskUtil.normalize(key);
81         ILispDAO table = getOrInstantiateVniTable(key);
82         table.put(eid, new MappingEntry<>(SubKeys.RECORD, value));
83         if (sourceRlocs != null) {
84             table.put(eid, new MappingEntry<>(SubKeys.SRC_RLOCS, sourceRlocs));
85         }
86     }
87
88     @Override
89     public void addMapping(Eid key, XtrId xtrId, Object value) {
90         Eid eid = MaskUtil.normalize(key);
91         ILispDAO table = getOrInstantiateVniTable(key);
92         ILispDAO xtrIdDao = getOrInstantiateXtrIdTable(eid, table);
93         xtrIdDao.put(xtrId, new MappingEntry<>(SubKeys.RECORD, value));
94     }
95
96     // Returns the mapping corresponding to the longest prefix match for eid. eid must be a simple (maskable or not)
97     // address
98     private Object getMappingLpmEid(Eid eid, XtrId xtrId, ILispDAO dao) {
99         SimpleImmutableEntry<Eid, Map<String, ?>> daoEntry = dao.getBestPair(MaskUtil.normalize(eid));
100         if (daoEntry != null) {
101             if (xtrId != null) {
102                 ILispDAO xtrIdTable = (ILispDAO) daoEntry.getValue().get(SubKeys.XTRID_RECORDS);
103                 if (xtrIdTable != null) {
104                     return xtrIdTable.getSpecific(xtrId, SubKeys.RECORD);
105                 }
106             } else {
107                 return daoEntry.getValue().get(SubKeys.RECORD);
108             }
109         }
110         return null;
111     }
112
113     @Override
114     public Object getMapping(Eid srcEid, Eid dstEid) {
115         final XtrId xtrId = null;
116         return getMapping(dstEid, xtrId);
117     }
118
119     @Override
120     public Object getMapping(Eid eid, XtrId xtrId) {
121         if (eid == null) {
122             return null;
123         }
124
125         ILispDAO table = getVniTable(eid);
126         if (table == null) {
127             return null;
128         }
129         return getMappingLpmEid(eid, xtrId, table);
130     }
131
132     // Returns the list of mappings stored in an xTR-ID DAO
133     private List<Object> getXtrIdMappingList(ILispDAO dao) {
134         if (dao != null) {
135             final List<Object> records = new ArrayList<>();
136             dao.getAll(new IRowVisitor() {
137                 public void visitRow(Object keyId, String valueKey, Object value) {
138                     if (valueKey.equals(SubKeys.RECORD)) {
139                         records.add(value);
140                     }
141                 }
142             });
143             return records;
144         }
145         return null;
146     }
147
148     @Override
149     public List<Object> getAllXtrIdMappings(Eid eid) {
150         ILispDAO table = getVniTable(eid);
151         if (table == null) {
152             return null;
153         }
154         Map<String, ?> daoEntry = table.getBest(MaskUtil.normalize(eid));
155         if (daoEntry != null) {
156             ILispDAO xtrIdTable = (ILispDAO) daoEntry.get(SubKeys.XTRID_RECORDS);
157             if (xtrIdTable != null) {
158                 return getXtrIdMappingList(xtrIdTable);
159             }
160         }
161         return null;
162     }
163
164     public Eid getWidestNegativeMapping(Eid eid) {
165         ILispDAO table = getVniTable(eid);
166         if (table == null) {
167             return null;
168         }
169         return table.getWidestNegativePrefix(MaskUtil.normalize(eid));
170     }
171
172     @Override
173     public Eid getParentPrefix(Eid eid) {
174         Eid key = MaskUtil.normalize(eid);
175         ILispDAO table = getVniTable(key);
176         if (table == null) {
177             return null;
178         }
179         return table.getParentPrefix(key);
180     }
181
182     @Override
183     public void removeMapping(Eid eid) {
184         Eid key = MaskUtil.normalize(eid);
185         ILispDAO table = getVniTable(key);
186         if (table == null) {
187             return;
188         }
189
190         // We intentionally don't remove subscribers, so in case a mapping is re-added, they get notified
191         table.removeSpecific(key, SubKeys.RECORD);
192         table.removeSpecific(key, SubKeys.SRC_RLOCS);
193         table.removeSpecific(key, SubKeys.XTRID_RECORDS);
194         table.removeSpecific(key, SubKeys.TIME_BUCKET_ID);
195     }
196
197     @Override
198     public void removeMapping(Eid eid, XtrId xtrId) {
199         Eid key = MaskUtil.normalize(eid);
200         ILispDAO table = getVniTable(key);
201         if (table == null) {
202             return;
203         }
204         ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
205         if (xtrIdTable == null) {
206             return;
207         }
208         xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
209     }
210
211     @Override
212     public void removeXtrIdMappings(Eid eid, List<XtrId> xtrIds) {
213         Eid key = MaskUtil.normalize(eid);
214         ILispDAO table = getVniTable(key);
215         if (table == null) {
216             return;
217         }
218         ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
219         if (xtrIdTable == null) {
220             return;
221         }
222         for (XtrId xtrId : xtrIds) {
223             xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
224         }
225     }
226
227     @Override
228     public void addData(Eid eid, String subKey, Object data) {
229         Eid key = MaskUtil.normalize(eid);
230         ILispDAO table = getOrInstantiateVniTable(key);
231         table.put(key, new MappingEntry<>(subKey, data));
232     }
233
234     @Override
235     public Object getData(Eid eid, String subKey) {
236         ILispDAO table = getOrInstantiateVniTable(eid);
237         if (table == null) {
238             return null;
239         }
240         Eid key = MaskUtil.normalize(eid);
241         return table.getSpecific(key, subKey);
242     }
243
244     @Override
245     public void removeData(Eid eid, String subKey) {
246         ILispDAO table = getOrInstantiateVniTable(eid);
247         if (table == null) {
248             return;
249         }
250         Eid key = MaskUtil.normalize(eid);
251         table.removeSpecific(key, subKey);
252     }
253
254     @Override
255     public String printMappings() {
256         final StringBuffer sb = new StringBuffer();
257         sb.append("Keys\tValues\n");
258
259         final IRowVisitor innerVisitor = (new IRowVisitor() {
260             String lastKey = "";
261
262             public void visitRow(Object keyId, String valueKey, Object value) {
263                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
264                 if (!lastKey.equals(key)) {
265                     sb.append("\n" + key + "\t");
266                 }
267                 sb.append(valueKey + "=" + value + "\t");
268                 lastKey = key;
269             }
270         });
271
272         dao.getAll(new IRowVisitor() {
273             String lastKey = "";
274
275             public void visitRow(Object keyId, String valueKey, Object value) {
276                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
277                 if (!lastKey.equals(key)) {
278                     sb.append("\n" + key + "\t");
279                 }
280                 if (valueKey.equals(SubKeys.VNI)) {
281                     sb.append(valueKey + "= { ");
282                     ((ILispDAO)value).getAll(innerVisitor);
283                     sb.append("}\t");
284                 } else {
285                     sb.append(valueKey + "=" + value + "\t");
286                 }
287                 lastKey = key;
288             }
289         });
290         sb.append("\n");
291         return sb.toString();
292     }
293 }