Make 'keys' CLI output user friendly 33/60533/1
authorLorand Jakab <lojakab@cisco.com>
Wed, 14 Jun 2017 13:04:09 +0000 (16:04 +0300)
committerLorand Jakab <lojakab@cisco.com>
Tue, 18 Jul 2017 11:45:19 +0000 (14:45 +0300)
Before this patch, the 'keys' CLI command would print out the full
Java objects contained in a map-cache. That's great for in-depth
debugging, but for a quick look it very hard to read. This patch
implements a user friendly CLI output for keys, to improve
readability.

The old full output is still available in Karaf, by adding the '-d' or
'--debug' switch to the CLI command.

Example output:

---8<------------------------------------------------------------------------------
opendaylight-user@root>keys
Instance ID 0
  -> EID                                           HMAC Algorithm        Shared Key
     192.0.2.1/32                                  HmacSHA1              password
     192.0.2.1/32|192.0.2.2/32                     HmacSHA1              password
------------------------------------------------------------------------------>8---

Change-Id: Ibe95d0ee4318053df3bc9f30c09c0e01c1d00eba
Signed-off-by: Lorand Jakab <lojakab@cisco.com>
12 files changed:
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/mapcache/IAuthKeyDb.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/mapcache/IMappingSystem.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/mappingservice/IMappingService.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/mappingservice/IMappingServiceShell.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingServiceShell.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingSystem.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/authentication/LispKeyIDEnum.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/Stringifier.java
mappingservice/mapcache/src/main/java/org/opendaylight/lispflowmapping/mapcache/AuthKeyDb.java
mappingservice/mapcache/src/main/java/org/opendaylight/lispflowmapping/mapcache/lisp/LispMapCacheStringifier.java
mappingservice/shell/src/main/java/org/opendaylight/lispflowmapping/shell/LispKeys.java

index b25070ab6e192b80028fbfb8a8b34a4b2c008d9a..99ef4be08c78973598a8c1878ab70a4af58e5585 100644 (file)
@@ -52,4 +52,11 @@ public interface IAuthKeyDb {
      * @return a String consisting of all the authentication keys in the database
      */
     String printKeys();
+
+    /**
+     * Print keys in cache in a human friendly format.
+     *
+     * @return a String consisting of all the keys in the cache
+     */
+    String prettyPrintKeys();
 }
index 32ada32c624862c94edada6a35ff7906f70a2a42..556663ae547bec0b54861222aa39c85ce2e282c5 100644 (file)
@@ -234,6 +234,13 @@ public interface IMappingSystem {
      */
     String printKeys();
 
+    /**
+     * Print keys in cache in a human friendly format.
+     *
+     * @return a String consisting of all the keys in the cache
+     */
+    String prettyPrintKeys();
+
     /**
      * Set cluster master status.
      *
index 5f08aa619083ddec68e583fde79fe422de95c90a..d515222446a3f79d87bf5f3314af969737585ffc 100644 (file)
@@ -227,6 +227,13 @@ public interface IMappingService {
      */
     String printKeys();
 
+    /**
+     * Print keys in cache in a human friendly format.
+     *
+     * @return a String consisting of all the keys in the cache
+     */
+    String prettyPrintKeys();
+
     /**
      * Cleans all cached mappings.Used for testing.
      */
index 54dbfc068ead7aa486579c4b50c19a0a5b9e99d4..4399d9a3e14f3361ee5eebbe65b82fb92d55a6a0 100644 (file)
@@ -36,6 +36,13 @@ public interface IMappingServiceShell {
      */
     String printKeys();
 
+    /**
+     * Print the full authentication key database in human readable form.
+     *
+     * @return the text to be printed on the Karaf console.
+     */
+    String prettyPrintKeys();
+
     /**
      * Add the default key "password" for the IPv4 prefix 0.0.0.0/0.
      */
index 0cd50e7632c4a801699b24acffe3ccd490fdbef1..14acd328e95884e34745b7bc9eb945ae9db159a1 100644 (file)
@@ -491,6 +491,11 @@ public class MappingService implements OdlMappingserviceService, IMappingService
         return mappingSystem.printKeys();
     }
 
+    @Override
+    public String prettyPrintKeys() {
+        return mappingSystem.prettyPrintKeys();
+    }
+
     @Override
     public void close() throws Exception {
         LOG.info("Mapping Service is being destroyed!");
index e1b356cea2fabda404ff318c18cd0dd05ce842e2..a8eac84c824eccadb30c7ddd6f85ea9b7b7ddedc 100644 (file)
@@ -48,6 +48,11 @@ public class MappingServiceShell implements IMappingServiceShell {
         return mappingService.printKeys();
     }
 
+    @Override
+    public String prettyPrintKeys() {
+        return mappingService.prettyPrintKeys();
+    }
+
     @Override
     public void addDefaultKeyIPv4() {
         Eid eid = LispAddressUtil.toEid(new Ipv4Prefix("0.0.0.0/0"), null);
index ef6f94060307c52f239919797a0ee5632262b8d4..52d95e34b0c90ad07c7f552187558bd3ce71d458 100644 (file)
@@ -688,6 +688,11 @@ public class MappingSystem implements IMappingSystem {
         return akdb.printKeys();
     }
 
+    @Override
+    public String prettyPrintKeys() {
+        return akdb.prettyPrintKeys();
+    }
+
     public void cleanCaches() {
         dao.removeAll();
         buildMapCaches();
index d99cee78a93bd302d56a84a1c3b79a0e0fa38be1..c0ded11be9387117fd10ac7453e9c053a6c105f7 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.lispflowmapping.lisp.authentication;
 
 public enum LispKeyIDEnum {
-    NONE(0, null),
+    NONE(0, "_NONE_"),
     SHA1(1, "HmacSHA1"),
     SHA256(2, "HmacSHA256"),
     UNKNOWN(-1, null);
index 1dad461648075165e2e82200a1e65e77d4363a63..4b8e92067cf24f19cb6c06baf1646751b76c37f9 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.lispflowmapping.lisp.util;
 
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
 
 /**
@@ -20,12 +21,30 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ma
 public class Stringifier {
     private static final String NEW_LINE = System.lineSeparator();
 
+    public static String getSpacesAsString(int length) {
+        return new String(new char[length]).replace("\0", " ");
+    }
+
+    public static String getString(MappingAuthkey key) {
+        return getString(key, 0);
+    }
+
+    public static String getString(MappingAuthkey key, int indentation) {
+        final String indent = getSpacesAsString(indentation);
+
+        StringBuilder masb = new StringBuilder(indent);
+        masb.append(key.getKeyString());
+        masb.append("   ");
+        masb.append(key.getKeyType());
+        return masb.toString();
+    }
+
     public static String getString(MappingRecord mapping) {
         return getString(mapping, 0);
     }
 
     public static String getString(MappingRecord mapping, int indentation) {
-        final String indent = new String(new char[indentation]).replace("\0", " ");
+        final String indent = getSpacesAsString(indentation);
 
         StringBuilder mrsb = new StringBuilder(indent);
 
@@ -68,14 +87,14 @@ public class Stringifier {
     }
 
     public static String getString(LocatorRecord locator, int indentation) {
-        final String indent = new String(new char[indentation]).replace("\0", " ");
+        final String indent = getSpacesAsString(indentation);
 
         StringBuilder lrsb = new StringBuilder(indent);
 
         String rloc = LispAddressStringifier.getString(locator.getRloc());
-        int padLen = Constants.INET6_ADDRSTRLEN + 2 - rloc.length();
+        int padLen = Math.max(2, Constants.INET6_ADDRSTRLEN + 2 - rloc.length());
         lrsb.append(rloc);
-        lrsb.append(new String(new char[padLen]).replace("\0", " "));
+        lrsb.append(getSpacesAsString(padLen));
         lrsb.append(locator.isRouted() ? "up        " : "no-route  ");
         lrsb.append(locator.getPriority().toString());
         lrsb.append('/');
index c4b20e91720903687b9dc8d5c65529d0f6a9e422..131da871fa047b8e80e8d295e8a5c2c51a858ef3 100644 (file)
@@ -121,4 +121,9 @@ public class AuthKeyDb implements IAuthKeyDb {
     public String printKeys() {
         return LispMapCacheStringifier.printKeys(dao);
     }
+
+    @Override
+    public String prettyPrintKeys() {
+        return LispMapCacheStringifier.prettyPrintKeys(dao);
+    }
 }
index 38d3fc0f7b388c3f1a8107ba0994b1dae0a65cec..505b12e638cb9293754afe3cc7bb2159e3bbe187 100644 (file)
@@ -12,10 +12,13 @@ import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
+import org.opendaylight.lispflowmapping.lisp.authentication.LispKeyIDEnum;
 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
 import org.opendaylight.lispflowmapping.lisp.util.Constants;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 import org.opendaylight.lispflowmapping.lisp.util.Stringifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
 
 public class LispMapCacheStringifier {
     public static String printKeys(ILispDAO dao) {
@@ -57,6 +60,50 @@ public class LispMapCacheStringifier {
         return sb.toString();
     }
 
+    @SuppressWarnings("unchecked")
+    public static String prettyPrintKeys(ILispDAO dao) {
+        final StringBuffer sb = new StringBuffer();
+
+        final IRowVisitor innerVisitor = (new IRowVisitor() {
+            public void visitRow(Object keyId, String valueKey, Object value) {
+                switch (valueKey) {
+                    case SubKeys.AUTH_KEY:
+                        String eid = LispAddressStringifier.getString((Eid) keyId);
+                        sb.append("     ");
+                        sb.append(eid);
+                        int padLen = Math.max(2, Constants.INET6_ADDRSTRLEN - eid.length());
+                        sb.append(Stringifier.getSpacesAsString(padLen));
+                        MappingAuthkey authKey = (MappingAuthkey) value;
+                        String hmac = LispKeyIDEnum.valueOf(authKey.getKeyType().shortValue()).getAuthenticationName();
+                        sb.append(hmac);
+                        sb.append(Stringifier.getSpacesAsString(Math.max(2, 22 - hmac.length())));
+                        sb.append(authKey.getKeyString());
+                        sb.append("\n");
+                        break;
+                    default:
+                        break;
+                }
+            }
+        });
+
+        dao.getAll(new IRowVisitor() {
+            String lastKey = "";
+
+            public void visitRow(Object keyId, String valueKey, Object value) {
+                String key = keyId.getClass().getSimpleName() + "#" + keyId;
+                if (!lastKey.equals(key)) {
+                    sb.append("Instance ID " + keyId + "\n");
+                    sb.append("  -> EID                                           HMAC Algorithm        Shared Key\n");
+                }
+                if (valueKey.equals(SubKeys.VNI)) {
+                    ((ILispDAO)value).getAll(innerVisitor);
+                }
+                lastKey = key;
+            }
+        });
+        return sb.toString();
+    }
+
     public static String printFMCMappings(ILispDAO dao) {
         final StringBuffer sb = new StringBuffer();
         sb.append("Keys\tValues\n");
index 34436e802da144e75e392a82c64978f1b41e664d..2a684e981a5ac55539146563dcdd02c7851c459d 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.lispflowmapping.shell;
 
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServiceShell;
 
@@ -20,6 +21,9 @@ import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServic
  */
 @Command(scope = "mappingservice", name = "keys", description = "Print LISP authentication keys")
 public class LispKeys  extends OsgiCommandSupport {
+    @Option(name = "-d", aliases = "--debug", description = "Debug output", required = false, multiValued = false)
+    private boolean debug;
+
     private IMappingServiceShell mappingServiceShell;
 
     /*
@@ -29,7 +33,11 @@ public class LispKeys  extends OsgiCommandSupport {
     @Override
     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
     protected Object doExecute() throws Exception {
-        System.out.print(mappingServiceShell.printKeys());
+        if (debug) {
+            System.out.print(mappingServiceShell.printKeys());
+        } else {
+            System.out.print(mappingServiceShell.prettyPrintKeys());
+        }
         return null;
     }