2 * Copyright (c) 2015, 2017 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.lispflowmapping.mapcache.lisp.LispMapCacheStringifier;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
28 * Simple map-cache that works with 'simple' addresses (see lisp-proto.yang). It can do longest prefix matching for IP
31 * @author Florin Coras
32 * @author Lorand Jakab
35 public class SimpleMapCache implements ILispMapCache {
38 public SimpleMapCache(ILispDAO dao) {
42 private ILispDAO getVniTable(Eid eid) {
44 if (eid.getVirtualNetworkId() == null) {
47 vni = eid.getVirtualNetworkId().getValue();
49 return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
52 private ILispDAO getOrInstantiateVniTable(Eid eid) {
54 if (eid.getVirtualNetworkId() == null) {
57 vni = eid.getVirtualNetworkId().getValue();
59 ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
61 table = dao.putNestedTable(vni, SubKeys.VNI);
66 private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
67 ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
69 table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
75 public void addMapping(Eid key, Object value) {
76 addMapping(key, value, null);
80 public void addMapping(Eid key, Object value, Set<IpAddressBinary> sourceRlocs) {
81 Eid eid = MaskUtil.normalize(key);
82 ILispDAO table = getOrInstantiateVniTable(key);
83 table.put(eid, new MappingEntry<>(SubKeys.RECORD, value));
84 if (sourceRlocs != null) {
85 table.put(eid, new MappingEntry<>(SubKeys.SRC_RLOCS, sourceRlocs));
90 public void addMapping(Eid key, XtrId xtrId, Object value) {
91 Eid eid = MaskUtil.normalize(key);
92 ILispDAO table = getOrInstantiateVniTable(key);
93 ILispDAO xtrIdDao = getOrInstantiateXtrIdTable(eid, table);
94 xtrIdDao.put(xtrId, new MappingEntry<>(SubKeys.RECORD, value));
97 // Returns the mapping corresponding to the longest prefix match for eid. eid must be a simple (maskable or not)
99 private Object getMappingLpmEid(Eid eid, XtrId xtrId, ILispDAO dao) {
100 SimpleImmutableEntry<Eid, Map<String, ?>> daoEntry = dao.getBestPair(MaskUtil.normalize(eid));
101 if (daoEntry != null) {
103 ILispDAO xtrIdTable = (ILispDAO) daoEntry.getValue().get(SubKeys.XTRID_RECORDS);
104 if (xtrIdTable != null) {
105 return xtrIdTable.getSpecific(xtrId, SubKeys.RECORD);
108 return daoEntry.getValue().get(SubKeys.RECORD);
115 public Object getMapping(Eid srcEid, Eid dstEid) {
116 final XtrId xtrId = null;
117 return getMapping(dstEid, xtrId);
121 public Object getMapping(Eid eid, XtrId xtrId) {
126 ILispDAO table = getVniTable(eid);
130 return getMappingLpmEid(eid, xtrId, table);
133 // Returns the list of mappings stored in an xTR-ID DAO
134 private List<Object> getXtrIdMappingList(ILispDAO dao) {
136 final List<Object> records = new ArrayList<>();
137 dao.getAll(new IRowVisitor() {
138 public void visitRow(Object keyId, String valueKey, Object value) {
139 if (valueKey.equals(SubKeys.RECORD)) {
150 public List<Object> getAllXtrIdMappings(Eid eid) {
151 ILispDAO table = getVniTable(eid);
155 Map<String, ?> daoEntry = table.getBest(MaskUtil.normalize(eid));
156 if (daoEntry != null) {
157 ILispDAO xtrIdTable = (ILispDAO) daoEntry.get(SubKeys.XTRID_RECORDS);
158 if (xtrIdTable != null) {
159 return getXtrIdMappingList(xtrIdTable);
165 public Eid getWidestNegativeMapping(Eid eid) {
166 ILispDAO table = getVniTable(eid);
170 return table.getWidestNegativePrefix(MaskUtil.normalize(eid));
174 public Eid getParentPrefix(Eid eid) {
175 ILispDAO table = getVniTable(eid);
179 return table.getParentPrefix(MaskUtil.normalize(eid));
183 public Eid getSiblingPrefix(Eid eid) {
184 ILispDAO table = getVniTable(eid);
188 return table.getSiblingPrefix(MaskUtil.normalize(eid));
192 public Eid getVirtualParentSiblingPrefix(Eid eid) {
193 ILispDAO table = getVniTable(eid);
197 return table.getVirtualParentSiblingPrefix(MaskUtil.normalize(eid));
201 public void removeMapping(Eid eid) {
202 ILispDAO table = getVniTable(eid);
207 Eid key = MaskUtil.normalize(eid);
212 public void removeMapping(Eid eid, XtrId xtrId) {
213 ILispDAO table = getVniTable(eid);
217 Eid key = MaskUtil.normalize(eid);
218 ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
219 if (xtrIdTable == null) {
222 xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
226 public void removeXtrIdMappings(Eid eid, List<XtrId> xtrIds) {
227 ILispDAO table = getVniTable(eid);
231 Eid key = MaskUtil.normalize(eid);
232 ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
233 if (xtrIdTable == null) {
236 for (XtrId xtrId : xtrIds) {
237 xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
242 public void addData(Eid eid, String subKey, Object data) {
243 ILispDAO table = getOrInstantiateVniTable(eid);
244 Eid key = MaskUtil.normalize(eid);
245 table.put(key, new MappingEntry<>(subKey, data));
249 public Object getData(Eid eid, String subKey) {
250 ILispDAO table = getOrInstantiateVniTable(eid);
254 Eid key = MaskUtil.normalize(eid);
255 return table.getSpecific(key, subKey);
259 public void removeData(Eid eid, String subKey) {
260 ILispDAO table = getOrInstantiateVniTable(eid);
264 Eid key = MaskUtil.normalize(eid);
265 table.removeSpecific(key, subKey);
269 public String printMappings() {
270 return LispMapCacheStringifier.printSMCMappings(dao);
274 public String prettyPrintMappings() {
275 return LispMapCacheStringifier.prettyPrintSMCMappings(dao);