Improving output for intents and intents -s

Change-Id: I32c4fb739b83fbce422bcd14c1755a5f9e284f75
diff --git a/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
index 59b3e47..e15845c 100644
--- a/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
@@ -15,14 +15,22 @@
  */
 package org.onosproject.cli.net;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.stream.StreamSupport;
-
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
 import org.onlab.util.StringFilter;
 import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.FilteredConnectPoint;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.intent.ConnectivityIntent;
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
@@ -37,10 +45,10 @@
 import org.onosproject.net.intent.PointToPointIntent;
 import org.onosproject.net.intent.SinglePointToMultiPointIntent;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.StreamSupport;
 
 /**
  * Lists the inventory of intents and their states.
@@ -49,6 +57,73 @@
          description = "Lists the inventory of intents and their states")
 public class IntentsListCommand extends AbstractShellCommand {
 
+    // Color codes and style
+    private static final String BOLD = "\u001B[1m";
+    private static final String RESET = "\u001B[0m";
+
+    // Messages and string formatter
+    private static final String APP_ID = BOLD + "Application Id:" + RESET + " %s";
+
+    private static final String COMMON_SELECTOR = BOLD + "Common ingress " +
+            "selector:" + RESET + " %s";
+
+    private static final String CP = BOLD + "Connect Point:" + RESET + " %s";
+
+    private static final String CONSTRAINTS = BOLD + "Constraints:" + RESET + " %s";
+
+    private static final String DST = BOLD + "Destination " + RESET;
+
+    private static final String EGRESS = BOLD + "Egress ";
+
+    private static final String FILTERED_CPS = "connect points and individual selectors" + RESET;
+
+    private static final String HOST = "host:" + RESET + " %s";
+
+    private static final String ID = BOLD + "Id:" + RESET + " %s";
+
+    private static final String INHERITED = "Inherited";
+
+    private static final String INGRESS = BOLD + "Ingress ";
+
+    private static final String INDENTATION = " -> ";
+
+    private static final String INSTALLABLE = BOLD + "Installable:" + RESET + " %s";
+
+    private static final String KEY = BOLD + "Key:" + RESET + " %s";
+
+    private static final String RESOURCES = BOLD + "Resources:" + RESET + " %s";
+
+    private static final String SELECTOR = BOLD + "Selector:" + RESET + " %s";
+
+    private static final String SEPARATOR = StringUtils.repeat("-", 172);;
+
+    private static final String SPACE = "   ";
+
+    private static final String SRC = BOLD + "Source ";
+
+    private static final String STATE = BOLD + "State:" + RESET + " %s";
+
+    private static final String TREATMENT = BOLD + "Treatment:" + RESET + " %s";
+
+    private static final String TYPE = BOLD + "Intent type:" + RESET + " %s";
+
+    private static final String SUMMARY_TITLES =
+            BOLD + String.format(
+            "\n%1s%21s%14s%14s%14s%14s%14s%14s%14s%14s%14s%14s",
+            "Intent type",
+            "Total",
+            "Installed",
+            "Withdrawn",
+            "Failed",
+            "InstallReq",
+            "Compiling",
+            "Installing",
+            "Recompiling",
+            "WithdrawReq",
+            "Withdrawing",
+            "UnknownState") +
+            RESET;
+
     @Option(name = "-i", aliases = "--installable",
             description = "Output Installable Intents",
             required = false, multiValued = false)
@@ -71,8 +146,6 @@
 
     private StringFilter contentFilter;
 
-    private String sep = System.lineSeparator();
-
     @Override
     protected void execute() {
         IntentService service = get(IntentService.class);
@@ -85,12 +158,12 @@
             if (outputJson()) {
                 print("%s", intentSummaries.json());
             } else {
-                intentSummaries.printSummary();
+                print(intentSummaries.summary());
             }
             return;
         } else if (pending) {
             if (outputJson()) {
-                print("%s", json(service, service.getPending()));
+                print("%s", json(service.getPending()));
             } else {
                 StreamSupport.stream(service.getPending().spliterator(), false)
                         .filter(intent -> contentFilter.filter(intent))
@@ -100,7 +173,7 @@
         }
 
         if (outputJson()) {
-            print("%s", json(service, service.getIntents()));
+            print("%s", json(service.getIntents()));
         } else {
             printIntents(service);
         }
@@ -124,7 +197,7 @@
         private IntentSummary summaryUnknownType;
 
         /**
-         * Initializes the internal state.
+         * Initializes the internal summary.
          */
         private void init() {
             summaryAll = new IntentSummary("All");
@@ -239,19 +312,24 @@
         /**
          * Prints summary of the intents.
          */
-        private void printSummary() {
-            summaryConnectivity.printState();
-            summaryHostToHost.printState();
-            summaryPointToPoint.printState();
-            summaryMultiPointToSinglePoint.printState();
-            summarySinglePointToMultiPoint.printState();
-            summaryPath.printState();
-            summaryLinkCollection.printState();
-            summaryOpticalCircuit.printState();
-            summaryOpticalConnectivity.printState();
-            summaryOpticalOdu.printState();
-            summaryUnknownType.printState();
-            summaryAll.printState();
+        private String summary() {
+            StringBuilder builder = new StringBuilder();
+            builder.append(SUMMARY_TITLES);
+            builder.append("\n" + SEPARATOR);
+            builder.append(summaryAll.summary());
+            builder.append(summaryPointToPoint.summary());
+            builder.append(summarySinglePointToMultiPoint.summary());
+            builder.append(summaryMultiPointToSinglePoint.summary());
+            builder.append(summaryHostToHost.summary());
+            builder.append(summaryLinkCollection.summary());
+            builder.append(summaryConnectivity.summary());
+            builder.append(summaryPath.summary());
+            builder.append(summaryOpticalCircuit.summary());
+            builder.append(summaryOpticalConnectivity.summary());
+            builder.append(summaryOpticalOdu.summary());
+            builder.append(summaryUnknownType.summary());
+
+            return builder.toString();
         }
 
         /**
@@ -271,19 +349,6 @@
             private int failed = 0;
             private int unknownState = 0;
 
-            private static final String FORMAT_SUMMARY_LINE1 =
-                "%-23s    total=        %7d   installed=   %7d";
-            private static final String FORMAT_SUMMARY_LINE2 =
-                "%-23s    withdrawn=    %7d   failed=      %7d";
-            private static final String FORMAT_SUMMARY_LINE3 =
-                "%-23s    installReq=   %7d   compiling=   %7d";
-            private static final String FORMAT_SUMMARY_LINE4 =
-                "%-23s    installing=   %7d   recompiling= %7d";
-            private static final String FORMAT_SUMMARY_LINE5 =
-                "%-23s    withdrawReq=  %7d   withdrawing= %7d";
-            private static final String FORMAT_SUMMARY_LINE6 =
-                "%-23s    unknownState= %7d";
-
             /**
              * Constructor.
              *
@@ -296,7 +361,7 @@
             /**
              * Updates the Intent Summary.
              *
-             * @param intentState the state of the Intent
+             * @param intentState the state of the intent
              */
             void update(IntentState intentState) {
                 total++;
@@ -336,21 +401,36 @@
 
             /**
              * Prints the Intent Summary.
+             *
              */
-            void printState() {
-                print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
-                print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
-                print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
-                print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
-                print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
-                if (unknownState != 0) {
-                    print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
-                }
+            String summary() {
+                StringBuilder builder = new StringBuilder();
+
+                builder.append(String.format(
+                        "\n%1s%s%14d%14d%14d%14d%14d%14d%14d%14d%14d%14d",
+                        BOLD + intentType + RESET,
+                        Strings.padStart(String.valueOf(total),
+                                         (32 - intentType.length()),
+                                         ' '),
+                        installed,
+                        withdrawn,
+                        failed,
+                        installReq,
+                        compiling,
+                        installing,
+                        recompiling,
+                        withdrawReq,
+                        withdrawing,
+                        unknownState));
+                builder.append("\n" + SEPARATOR);
+
+                return builder.toString();
             }
 
             /**
              * Gets the JSON representation of the Intent Summary.
              *
+             * @param mapper the object mapper
              * @return the JSON representation of the Intent Summary
              */
             JsonNode json(ObjectMapper mapper) {
@@ -359,8 +439,8 @@
                     .put("installed", installed)
                     .put("failed", failed)
                     .put("installReq", installReq)
-                    .put("compiling", compiling)
                     .put("installing", installing)
+                    .put("compiling", compiling)
                     .put("recompiling", recompiling)
                     .put("withdrawReq", withdrawReq)
                     .put("withdrawing", withdrawing)
@@ -372,79 +452,139 @@
         }
     }
 
+    /*
+     * Prints detailed information about a specific intent.
+     */
     private String detailsFormat(IntentService service, Intent intent) {
         StringBuilder builder = new StringBuilder();
         if (!intent.resources().isEmpty()) {
-            builder.append(String.format("    resources=%s%s", intent.resources(), sep));
+            builder.append("\n" + String.format(RESOURCES, intent.resources()));
         }
         if (intent instanceof ConnectivityIntent) {
             ConnectivityIntent ci = (ConnectivityIntent) intent;
             if (!ci.selector().criteria().isEmpty()) {
-                builder.append(String.format("    selector=%s%s", ci.selector().criteria(), sep));
+                builder.append("\n" + String.format(COMMON_SELECTOR, formatSelector(ci.selector())));
             }
             if (!ci.treatment().allInstructions().isEmpty()) {
-                builder.append(String.format("    treatment=%s%s", ci.treatment().allInstructions(), sep));
+                builder.append("\n" + String.format(TREATMENT, ci.treatment().allInstructions()));
             }
             if (ci.constraints() != null && !ci.constraints().isEmpty()) {
-                builder.append(String.format("    constraints=%s%s", ci.constraints(), sep));
+                builder.append("\n" + String.format(CONSTRAINTS, ci.constraints()));
             }
         }
 
         if (intent instanceof HostToHostIntent) {
             HostToHostIntent pi = (HostToHostIntent) intent;
-            builder.append(String.format("    host1=%s, host2=%s", pi.one(), pi.two()));
+            builder.append("\n" + String.format(SRC + HOST, pi.one()));
+            builder.append("\n" + String.format(DST + HOST, pi.two()));
         } else if (intent instanceof PointToPointIntent) {
             PointToPointIntent pi = (PointToPointIntent) intent;
-            builder.append(String.format("    ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint()));
+            builder.append("\n" + formatFilteredCps(Sets.newHashSet(pi.filteredIngressPoint()), INGRESS));
+            builder.append("\n" + formatFilteredCps(Sets.newHashSet(pi.filteredEgressPoint()), EGRESS));
         } else if (intent instanceof MultiPointToSinglePointIntent) {
             MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
-            builder.append(String.format("    ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint()));
+            builder.append("\n" + formatFilteredCps(pi.filteredIngressPoints(), INGRESS));
+            builder.append("\n" + formatFilteredCps(Sets.newHashSet(pi.filteredEgressPoint()), EGRESS));
         } else if (intent instanceof SinglePointToMultiPointIntent) {
             SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
-            builder.append(String.format("    ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints()));
+            builder.append("\n" + formatFilteredCps(Sets.newHashSet(pi.filteredIngressPoint()), INGRESS));
+            builder.append("\n" + formatFilteredCps(pi.filteredEgressPoints(), EGRESS));
         } else if (intent instanceof PathIntent) {
             PathIntent pi = (PathIntent) intent;
-            builder.append(String.format("    path=%s, cost=%f", pi.path().links(), pi.path().cost()));
+            builder.append(String.format("path=%s, cost=%f", pi.path().links(), pi.path().cost()));
         } else if (intent instanceof LinkCollectionIntent) {
             LinkCollectionIntent li = (LinkCollectionIntent) intent;
-            builder.append(String.format("    links=%s", li.links()));
-            builder.append(String.format("    egress=%s", li.egressPoints()));
+            builder.append("\n" + String.format("links=%s", li.links()));
+            builder.append("\n" + String.format(CP, li.egressPoints()));
         } else if (intent instanceof OpticalCircuitIntent) {
             OpticalCircuitIntent ci = (OpticalCircuitIntent) intent;
-            builder.append(String.format("    src=%s, dst=%s", ci.getSrc(), ci.getDst()));
+            builder.append("\n" + String.format("src=%s, dst=%s", ci.getSrc(), ci.getDst()));
         } else if (intent instanceof OpticalConnectivityIntent) {
             OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent;
-            builder.append(String.format("    src=%s, dst=%s", ci.getSrc(), ci.getDst()));
+            builder.append("\n" + String.format("src=%s, dst=%s", ci.getSrc(), ci.getDst()));
         } else if (intent instanceof OpticalOduIntent) {
             OpticalOduIntent ci = (OpticalOduIntent) intent;
-            builder.append(String.format("    src=%s, dst=%s", ci.getSrc(), ci.getDst()));
+            builder.append("\n" + String.format("src=%s, dst=%s", ci.getSrc(), ci.getDst()));
         }
 
         List<Intent> installable = service.getInstallableIntents(intent.key());
         installable.stream().filter(i -> contentFilter.filter(i));
         if (showInstallable && installable != null && !installable.isEmpty()) {
-            builder.append(String.format("%s    installable=%s", sep, installable));
+            builder.append("\n" + String.format(INSTALLABLE, installable));
         }
         return builder.toString();
     }
 
+    /*
+     * Prints out a formatted string, given a list of connect points.
+     */
+    private String formatFilteredCps(Set<FilteredConnectPoint> fCps, String prefix) {
+        StringBuilder builder = new StringBuilder();
+        builder.append(prefix);
+        builder.append(FILTERED_CPS);
+        fCps.forEach(fCp -> builder.append("\n" + String.format(formatFilteredCp(fCp))));
+
+        return builder.toString();
+    }
+
+    /*
+     * Prints out a formatted string, given a filtered connect point.
+     */
+    private String formatFilteredCp(FilteredConnectPoint fCp) {
+        ConnectPoint connectPoint = fCp.connectPoint();
+        TrafficSelector selector = fCp.trafficSelector();
+        StringBuilder builder = new StringBuilder();
+        builder.append(INDENTATION + String.format(CP, connectPoint));
+        builder.append(SPACE + String.format(SELECTOR, formatSelector(selector)));
+
+        return builder.toString();
+    }
+
+    /*
+     * Prints out a formatted string, given a traffic selector
+     */
+    private String formatSelector(TrafficSelector ts) {
+        StringBuilder builder = new StringBuilder();
+        List<Criterion> criteria = Lists.newArrayList(ts.criteria());
+
+        if (criteria == null || criteria.isEmpty()) {
+            builder.append(INHERITED);
+            return builder.toString();
+        }
+
+        criteria.forEach(c -> {
+            builder.append(c.toString());
+            if (criteria.indexOf(c) < criteria.size() - 1) {
+                builder.append(", ");
+            }
+        });
+
+        return builder.toString();
+    }
+
     private String fullFormat(Intent intent) {
         return fullFormat(intent, null);
     }
 
+    /*
+     * Prints information about the intent state, given an intent.
+     */
     private String fullFormat(Intent intent, String state) {
         StringBuilder builder = new StringBuilder();
-        builder.append(String.format("id=%s, ", intent.id()));
+        builder.append(String.format(ID, intent.id()));
         if (state != null) {
-            builder.append(String.format("state=%s, ", state));
+            builder.append("\n" + String.format(STATE, state));
         }
-        builder.append(String.format("key=%s, type=%s, appId=%s",
-                                     intent.key(),
-                                     intent.getClass().getSimpleName(),
-                                     intent.appId().name()));
+        builder.append("\n" + String.format(KEY, intent.key()));
+        builder.append("\n" + String.format(TYPE, intent.getClass().getSimpleName()));
+        builder.append("\n" + String.format(APP_ID, intent.appId().name()));
+
         return builder.toString();
     }
 
+    /*
+     * Prints a detailed information about intents.
+     */
     private void printIntents(IntentService service) {
         for (Intent intent : service.getIntents()) {
             IntentState state = service.getIntentState(intent.key());
@@ -452,14 +592,19 @@
             String detailsIntentFormat = detailsFormat(service, intent);
             if (state != null && (contentFilter.filter(
                     intentFormat + detailsIntentFormat))) {
-                print(intentFormat);
-                print(detailsIntentFormat);
+                StringBuilder builder = new StringBuilder();
+                builder.append(intentFormat)
+                       .append(detailsIntentFormat)
+                       .append("\n");
+                print(builder.toString());
             }
         }
     }
 
-    // Produces JSON array of the specified intents.
-    private JsonNode json(IntentService service, Iterable<Intent> intents) {
+    /*
+     * Produces a JSON array from the intents specified.
+     */
+    private JsonNode json(Iterable<Intent> intents) {
         ObjectMapper mapper = new ObjectMapper();
         ArrayNode result = mapper.createArrayNode();
         StreamSupport.stream(intents.spliterator(), false)