2 * Copyright (c) 2017 Cisco Systems, Inc. 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
8 package org.opendaylight.lispflowmapping.mapcache.lisp;
11 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
12 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
13 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
14 import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
15 import org.opendaylight.lispflowmapping.lisp.authentication.LispKeyIDEnum;
16 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
17 import org.opendaylight.lispflowmapping.lisp.util.Constants;
18 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
19 import org.opendaylight.lispflowmapping.lisp.util.Stringifier;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
23 public class LispMapCacheStringifier {
24 public static String printKeys(ILispDAO dao) {
25 final StringBuilder sb = new StringBuilder();
26 sb.append("Keys\tValues\n");
28 final IRowVisitor innerVisitor = (new IRowVisitor() {
31 public void visitRow(Object keyId, String valueKey, Object value) {
32 String key = keyId.getClass().getSimpleName() + "#" + keyId;
33 if (!lastKey.equals(key)) {
34 sb.append("\n" + key + "\t");
36 sb.append(valueKey + "=" + value + "\t");
41 dao.getAll(new IRowVisitor() {
44 public void visitRow(Object keyId, String valueKey, Object value) {
45 String key = keyId.getClass().getSimpleName() + "#" + keyId;
46 if (!lastKey.equals(key)) {
47 sb.append("\n" + key + "\t");
49 if (valueKey.equals(SubKeys.VNI)) {
50 sb.append(valueKey + "= { ");
51 ((ILispDAO)value).getAll(innerVisitor);
54 sb.append(valueKey + "=" + value + "\t");
63 @SuppressWarnings("unchecked")
64 public static String prettyPrintKeys(ILispDAO dao) {
65 final StringBuilder sb = new StringBuilder();
67 final IRowVisitor innerVisitor = (new IRowVisitor() {
68 public void visitRow(Object keyId, String valueKey, Object value) {
70 case SubKeys.AUTH_KEY:
71 String eid = LispAddressStringifier.getString((Eid) keyId);
74 int padLen = Math.max(2, Constants.INET6_ADDRSTRLEN - eid.length());
75 sb.append(Stringifier.getSpacesAsString(padLen));
76 MappingAuthkey authKey = (MappingAuthkey) value;
77 String hmac = LispKeyIDEnum.valueOf(authKey.getKeyType().shortValue()).getAuthenticationName();
79 sb.append(Stringifier.getSpacesAsString(Math.max(2, 22 - hmac.length())));
80 sb.append(authKey.getKeyString());
89 dao.getAll(new IRowVisitor() {
92 public void visitRow(Object keyId, String valueKey, Object value) {
93 String key = keyId.getClass().getSimpleName() + "#" + keyId;
94 if (!lastKey.equals(key)) {
95 sb.append("Instance ID " + keyId + "\n");
96 sb.append(" -> EID HMAC Algorithm Shared Key\n");
98 if (valueKey.equals(SubKeys.VNI)) {
99 ((ILispDAO)value).getAll(innerVisitor);
104 return sb.toString();
107 public static String printFMCMappings(ILispDAO dao) {
108 final StringBuilder sb = new StringBuilder();
109 sb.append("Keys\tValues\n");
110 dao.getAll(new IRowVisitor() {
113 public void visitRow(Object keyId, String valueKey, Object value) {
114 String key = keyId.getClass().getSimpleName() + "#" + keyId;
115 if (!lastKey.equals(key)) {
116 sb.append("\n" + key + "\t");
118 sb.append(valueKey + "=" + value + "\t");
123 return sb.toString();
126 public static String prettyPrintFMCMappings(ILispDAO dao) {
127 final StringBuilder sb = new StringBuilder();
128 dao.getAll(new IRowVisitor() {
129 public void visitRow(Object keyId, String valueKey, Object value) {
132 MappingData md = (MappingData) value;
133 sb.append(Stringifier.getString(md.getRecord(), 2));
142 return sb.toString();
145 public static String printMTMCMappings(ILispDAO dao) {
146 final StringBuilder sb = new StringBuilder();
147 sb.append("Keys\tValues\n");
148 final IRowVisitor innerVisitor = (new IRowVisitor() {
151 public void visitRow(Object keyId, String valueKey, Object value) {
152 String key = keyId.getClass().getSimpleName() + "#" + keyId;
153 if (!lastKey.equals(key)) {
154 sb.append("\n" + key + "\t");
156 sb.append(valueKey + "=" + value + "\t");
160 final IRowVisitor vniVisitor = (new IRowVisitor() {
163 public void visitRow(Object keyId, String valueKey, Object value) {
164 String key = keyId.getClass().getSimpleName() + "#" + keyId;
165 if (!lastKey.equals(key)) {
166 sb.append(key + "\t");
168 if ((valueKey.equals(SubKeys.LCAF_SRCDST))) {
169 sb.append(valueKey + "= { ");
170 ((ILispDAO)value).getAll(innerVisitor);
173 sb.append(valueKey + "=" + value + "\t");
178 dao.getAll(new IRowVisitor() {
181 public void visitRow(Object keyId, String valueKey, Object value) {
182 String key = keyId.getClass().getSimpleName() + "#" + keyId;
183 if (!lastKey.equals(key)) {
184 sb.append("\n" + key + "\t");
186 if (valueKey.equals(SubKeys.VNI)) {
187 sb.append(valueKey + "= { ");
188 ((ILispDAO)value).getAll(vniVisitor);
191 sb.append(valueKey + "=" + value + "\t");
197 return sb.toString();
200 @SuppressWarnings("unchecked")
201 public static String prettyPrintMTMCMappings(ILispDAO dao) {
202 final StringBuilder sb = new StringBuilder();
204 final IRowVisitor mappingVisitor = (new IRowVisitor() {
205 public void visitRow(Object keyId, String valueKey, Object value) {
208 MappingData md = (MappingData) value;
209 sb.append(Stringifier.getString(md.getRecord(), 2));
212 case SubKeys.SUBSCRIBERS:
213 Set<Subscriber> subscribers = (Set<Subscriber>) value;
214 sb.append(prettyPrintSubscriberSet(subscribers, 4));
217 case SubKeys.LCAF_SRCDST:
218 ((ILispDAO)value).getAll(this);
226 dao.getAll(new IRowVisitor() {
229 public void visitRow(Object keyId, String valueKey, Object value) {
230 String key = keyId.getClass().getSimpleName() + "#" + keyId;
231 if (!lastKey.equals(key)) {
232 sb.append("Instance ID " + keyId + "\n");
234 if (valueKey.equals(SubKeys.VNI)) {
235 ((ILispDAO)value).getAll(mappingVisitor);
240 return sb.toString();
243 public static String printSMCMappings(ILispDAO dao) {
244 final StringBuilder sb = new StringBuilder();
245 sb.append("Keys\tValues\n");
247 final IRowVisitor innerVisitor = (new IRowVisitor() {
250 public void visitRow(Object keyId, String valueKey, Object value) {
251 String key = keyId.getClass().getSimpleName() + "#" + keyId;
252 if (!lastKey.equals(key)) {
253 sb.append("\n" + key + "\t");
255 sb.append(valueKey + "=" + value + "\t");
260 dao.getAll(new IRowVisitor() {
263 public void visitRow(Object keyId, String valueKey, Object value) {
264 String key = keyId.getClass().getSimpleName() + "#" + keyId;
265 if (!lastKey.equals(key)) {
266 sb.append("\n" + key + "\t");
268 if (valueKey.equals(SubKeys.VNI)) {
269 sb.append(valueKey + "= { ");
270 ((ILispDAO)value).getAll(innerVisitor);
273 sb.append(valueKey + "=" + value + "\t");
279 return sb.toString();
282 @SuppressWarnings("unchecked")
283 public static String prettyPrintSMCMappings(ILispDAO dao) {
284 final StringBuilder sb = new StringBuilder();
286 final IRowVisitor mappingVisitor = (new IRowVisitor() {
287 public void visitRow(Object keyId, String valueKey, Object value) {
290 MappingData md = (MappingData) value;
291 sb.append(Stringifier.getString(md.getRecord(), 2));
294 case SubKeys.SUBSCRIBERS:
295 Set<Subscriber> subscribers = (Set<Subscriber>) value;
296 sb.append(prettyPrintSubscriberSet(subscribers, 4));
305 dao.getAll(new IRowVisitor() {
308 public void visitRow(Object keyId, String valueKey, Object value) {
309 String key = keyId.getClass().getSimpleName() + "#" + keyId;
310 if (!lastKey.equals(key)) {
311 sb.append("Instance ID " + keyId + "\n");
313 if (valueKey.equals(SubKeys.VNI)) {
314 ((ILispDAO)value).getAll(mappingVisitor);
319 return sb.toString();
323 * Given a Set of Subscriber objects, and the level of indentation, create a nicely formatted String to be added
324 * to a map-cache print-out in a tabular form.
326 * @param subscribers the Set of Subscriber objects to be printed
327 * @param indentation indentation level
328 * @return the formatted String
330 public static String prettyPrintSubscriberSet(Set<Subscriber> subscribers, int indentation) {
331 final String indent = new String(new char[indentation]).replace("\0", " ");
333 StringBuilder sb = new StringBuilder(indent);
334 sb.append(" -----------------------------------------------------------------\n");
337 if (subscribers == null) {
338 return sb.append(" No subscribers").toString();
341 sb.append("-> Subscriber RLOC Subscriber EID\n ");
343 boolean first = true;
344 for (Subscriber subscriber : subscribers) {
351 String srcRloc = LispAddressStringifier.getString(subscriber.getSrcRloc());
352 int padLen = Constants.INET6_ADDRSTRLEN + 2 - srcRloc.length();
354 sb.append(new String(new char[padLen]).replace("\0", " "));
355 sb.append(LispAddressStringifier.getString(subscriber.getSrcEid()));
357 return sb.toString();