Make 'mappings' CLI output user friendly
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / SimpleMapCache.java
1 /*
2  * Copyright (c) 2015, 2017 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.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;
26
27 /**
28  * Simple map-cache that works with 'simple' addresses (see lisp-proto.yang). It can do longest prefix matching for IP
29  * addresses.
30  *
31  * @author Florin Coras
32  * @author Lorand Jakab
33  *
34  */
35 public class SimpleMapCache implements ILispMapCache {
36     private ILispDAO dao;
37
38     public SimpleMapCache(ILispDAO dao) {
39         this.dao = dao;
40     }
41
42     private ILispDAO getVniTable(Eid eid) {
43         long vni = 0;
44         if (eid.getVirtualNetworkId() == null) {
45             vni = 0;
46         } else {
47             vni = eid.getVirtualNetworkId().getValue();
48         }
49         return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
50     }
51
52     private ILispDAO getOrInstantiateVniTable(Eid eid) {
53         long vni = 0;
54         if (eid.getVirtualNetworkId() == null) {
55             vni = 0;
56         } else {
57             vni = eid.getVirtualNetworkId().getValue();
58         }
59         ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
60         if (table == null) {
61             table = dao.putNestedTable(vni, SubKeys.VNI);
62         }
63         return table;
64     }
65
66     private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
67         ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
68         if (table == null) {
69             table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
70         }
71         return table;
72     }
73
74     @Override
75     public void addMapping(Eid key, Object value) {
76         addMapping(key, value, null);
77     }
78
79     @Override
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));
86         }
87     }
88
89     @Override
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));
95     }
96
97     // Returns the mapping corresponding to the longest prefix match for eid. eid must be a simple (maskable or not)
98     // address
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) {
102             if (xtrId != null) {
103                 ILispDAO xtrIdTable = (ILispDAO) daoEntry.getValue().get(SubKeys.XTRID_RECORDS);
104                 if (xtrIdTable != null) {
105                     return xtrIdTable.getSpecific(xtrId, SubKeys.RECORD);
106                 }
107             } else {
108                 return daoEntry.getValue().get(SubKeys.RECORD);
109             }
110         }
111         return null;
112     }
113
114     @Override
115     public Object getMapping(Eid srcEid, Eid dstEid) {
116         final XtrId xtrId = null;
117         return getMapping(dstEid, xtrId);
118     }
119
120     @Override
121     public Object getMapping(Eid eid, XtrId xtrId) {
122         if (eid == null) {
123             return null;
124         }
125
126         ILispDAO table = getVniTable(eid);
127         if (table == null) {
128             return null;
129         }
130         return getMappingLpmEid(eid, xtrId, table);
131     }
132
133     // Returns the list of mappings stored in an xTR-ID DAO
134     private List<Object> getXtrIdMappingList(ILispDAO dao) {
135         if (dao != null) {
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)) {
140                         records.add(value);
141                     }
142                 }
143             });
144             return records;
145         }
146         return null;
147     }
148
149     @Override
150     public List<Object> getAllXtrIdMappings(Eid eid) {
151         ILispDAO table = getVniTable(eid);
152         if (table == null) {
153             return null;
154         }
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);
160             }
161         }
162         return null;
163     }
164
165     public Eid getWidestNegativeMapping(Eid eid) {
166         ILispDAO table = getVniTable(eid);
167         if (table == null) {
168             return null;
169         }
170         return table.getWidestNegativePrefix(MaskUtil.normalize(eid));
171     }
172
173     @Override
174     public Eid getParentPrefix(Eid eid) {
175         ILispDAO table = getVniTable(eid);
176         if (table == null) {
177             return null;
178         }
179         return table.getParentPrefix(MaskUtil.normalize(eid));
180     }
181
182     @Override
183     public Eid getSiblingPrefix(Eid eid) {
184         ILispDAO table = getVniTable(eid);
185         if (table == null) {
186             return null;
187         }
188         return table.getSiblingPrefix(MaskUtil.normalize(eid));
189     }
190
191     @Override
192     public Eid getVirtualParentSiblingPrefix(Eid eid) {
193         ILispDAO table = getVniTable(eid);
194         if (table == null) {
195             return null;
196         }
197         return table.getVirtualParentSiblingPrefix(MaskUtil.normalize(eid));
198     }
199
200     @Override
201     public void removeMapping(Eid eid) {
202         ILispDAO table = getVniTable(eid);
203         if (table == null) {
204             return;
205         }
206
207         Eid key = MaskUtil.normalize(eid);
208         table.remove(key);
209     }
210
211     @Override
212     public void removeMapping(Eid eid, XtrId xtrId) {
213         ILispDAO table = getVniTable(eid);
214         if (table == null) {
215             return;
216         }
217         Eid key = MaskUtil.normalize(eid);
218         ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
219         if (xtrIdTable == null) {
220             return;
221         }
222         xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
223     }
224
225     @Override
226     public void removeXtrIdMappings(Eid eid, List<XtrId> xtrIds) {
227         ILispDAO table = getVniTable(eid);
228         if (table == null) {
229             return;
230         }
231         Eid key = MaskUtil.normalize(eid);
232         ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
233         if (xtrIdTable == null) {
234             return;
235         }
236         for (XtrId xtrId : xtrIds) {
237             xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
238         }
239     }
240
241     @Override
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));
246     }
247
248     @Override
249     public Object getData(Eid eid, String subKey) {
250         ILispDAO table = getOrInstantiateVniTable(eid);
251         if (table == null) {
252             return null;
253         }
254         Eid key = MaskUtil.normalize(eid);
255         return table.getSpecific(key, subKey);
256     }
257
258     @Override
259     public void removeData(Eid eid, String subKey) {
260         ILispDAO table = getOrInstantiateVniTable(eid);
261         if (table == null) {
262             return;
263         }
264         Eid key = MaskUtil.normalize(eid);
265         table.removeSpecific(key, subKey);
266     }
267
268     @Override
269     public String printMappings() {
270         return LispMapCacheStringifier.printSMCMappings(dao);
271     }
272
273     @Override
274     public String prettyPrintMappings() {
275         return LispMapCacheStringifier.prettyPrintSMCMappings(dao);
276     }
277 }