2 * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.mapcache;
11 import java.util.AbstractMap.SimpleImmutableEntry;
12 import java.util.ArrayList;
13 import java.util.List;
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;
27 * Simple map-cache that works with 'simple' addresses (see lisp-proto.yang). It can do longest prefix matching for IP
30 * @author Florin Coras
31 * @author Lorand Jakab
34 public class SimpleMapCache implements ILispMapCache {
37 public SimpleMapCache(ILispDAO dao) {
41 private ILispDAO getVniTable(Eid eid) {
43 if (eid.getVirtualNetworkId() == null) {
46 vni = eid.getVirtualNetworkId().getValue();
48 return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
51 private ILispDAO getOrInstantiateVniTable(Eid eid) {
53 if (eid.getVirtualNetworkId() == null) {
56 vni = eid.getVirtualNetworkId().getValue();
58 ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
60 table = dao.putNestedTable(vni, SubKeys.VNI);
65 private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
66 ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
68 table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
74 public void addMapping(Eid key, Object value) {
75 addMapping(key, value, null);
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));
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));
96 // Returns the mapping corresponding to the longest prefix match for eid. eid must be a simple (maskable or not)
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) {
102 ILispDAO xtrIdTable = (ILispDAO) daoEntry.getValue().get(SubKeys.XTRID_RECORDS);
103 if (xtrIdTable != null) {
104 return xtrIdTable.getSpecific(xtrId, SubKeys.RECORD);
107 return daoEntry.getValue().get(SubKeys.RECORD);
114 public Object getMapping(Eid srcEid, Eid dstEid) {
115 final XtrId xtrId = null;
116 return getMapping(dstEid, xtrId);
120 public Object getMapping(Eid eid, XtrId xtrId) {
125 ILispDAO table = getVniTable(eid);
129 return getMappingLpmEid(eid, xtrId, table);
132 // Returns the list of mappings stored in an xTR-ID DAO
133 private List<Object> getXtrIdMappingList(ILispDAO dao) {
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)) {
149 public List<Object> getAllXtrIdMappings(Eid eid) {
150 ILispDAO table = getVniTable(eid);
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);
164 public Eid getWidestNegativeMapping(Eid eid) {
165 ILispDAO table = getVniTable(eid);
169 return table.getWidestNegativePrefix(MaskUtil.normalize(eid));
173 public Eid getParentPrefix(Eid eid) {
174 Eid key = MaskUtil.normalize(eid);
175 ILispDAO table = getVniTable(key);
179 return table.getParentPrefix(key);
183 public void removeMapping(Eid eid) {
184 Eid key = MaskUtil.normalize(eid);
185 ILispDAO table = getVniTable(key);
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);
197 public void removeMapping(Eid eid, XtrId xtrId) {
198 Eid key = MaskUtil.normalize(eid);
199 ILispDAO table = getVniTable(key);
203 ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
204 if (xtrIdTable == null) {
207 xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
211 public void removeXtrIdMappings(Eid eid, List<XtrId> xtrIds) {
212 Eid key = MaskUtil.normalize(eid);
213 ILispDAO table = getVniTable(key);
217 ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
218 if (xtrIdTable == null) {
221 for (XtrId xtrId : xtrIds) {
222 xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
227 public void addData(Eid eid, String subKey, Object data) {
228 Eid key = MaskUtil.normalize(eid);
229 ILispDAO table = getOrInstantiateVniTable(key);
230 table.put(key, new MappingEntry<>(subKey, data));
234 public Object getData(Eid eid, String subKey) {
235 ILispDAO table = getOrInstantiateVniTable(eid);
239 Eid key = MaskUtil.normalize(eid);
240 return table.getSpecific(key, subKey);
244 public void removeData(Eid eid, String subKey) {
245 ILispDAO table = getOrInstantiateVniTable(eid);
249 Eid key = MaskUtil.normalize(eid);
250 table.removeSpecific(key, subKey);
254 public String printMappings() {
255 final StringBuffer sb = new StringBuffer();
256 sb.append("Keys\tValues\n");
258 final IRowVisitor innerVisitor = (new IRowVisitor() {
261 public void visitRow(Object keyId, String valueKey, Object value) {
262 String key = keyId.getClass().getSimpleName() + "#" + keyId;
263 if (!lastKey.equals(key)) {
264 sb.append("\n" + key + "\t");
266 sb.append(valueKey + "=" + value + "\t");
271 dao.getAll(new IRowVisitor() {
274 public void visitRow(Object keyId, String valueKey, Object value) {
275 String key = keyId.getClass().getSimpleName() + "#" + keyId;
276 if (!lastKey.equals(key)) {
277 sb.append("\n" + key + "\t");
279 if (valueKey.equals(SubKeys.VNI)) {
280 sb.append(valueKey + "= { ");
281 ((ILispDAO)value).getAll(innerVisitor);
284 sb.append(valueKey + "=" + value + "\t");
290 return sb.toString();