Refactor k8s-node and k8s-networking command line interfaces

Change-Id: I1eb20ccea5238893a1c58e7faeb4c3a06accd2a9
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java
index bca34f2..4a69e10 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java
@@ -121,4 +121,21 @@
     public static final int POD_RESOLUTION_TABLE = 11;
 
     public static final int LOCAL_ENTRY_TABLE = 0;
+
+    // CLI item length
+    public static final int CLI_ID_LENGTH = 30;
+    public static final int CLI_NAME_LENGTH = 30;
+    public static final int CLI_IP_ADDRESSES_LENGTH = 50;
+    public static final int CLI_IP_ADDRESS_LENGTH = 25;
+    public static final int CLI_MAC_ADDRESS_LENGTH = 25;
+    public static final int CLI_PORTS_LENGTH = 20;
+    public static final int CLI_NAMESPACE_LENGTH = 15;
+    public static final int CLI_PHASE_LENGTH = 15;
+    public static final int CLI_TYPE_LENGTH = 15;
+    public static final int CLI_TYPES_LENGTH = 30;
+    public static final int CLI_SEG_ID_LENGTH = 10;
+    public static final int CLI_LABELS_LENGTH = 30;
+    public static final int CLI_CONTAINERS_LENGTH = 30;
+
+    public static final int CLI_MARGIN_LENGTH = 2;
 }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sEndpointsListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sEndpointsListCommand.java
index 5258c18..c866f04 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sEndpointsListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sEndpointsListCommand.java
@@ -18,9 +18,11 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.Endpoints;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -30,6 +32,11 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -40,7 +47,6 @@
         description = "Lists all kubernetes endpoints")
 public class K8sEndpointsListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-50s%-20s";
     private static final String PORT_PROTOCOL_SEPARATOR = "/";
 
     @Override
@@ -49,10 +55,13 @@
         List<Endpoints> endpointses = Lists.newArrayList(service.endpointses());
         endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_IP_ADDRESSES_LENGTH, CLI_PORTS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(endpointses));
         } else {
-            print(FORMAT, "Name", "IP Addresses", "Ports");
+            print(format, "Name", "IP Addresses", "Ports");
 
             for (Endpoints endpoints : endpointses) {
 
@@ -65,9 +74,11 @@
                             PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
                 });
 
-                print(FORMAT,
-                        endpoints.getMetadata().getName(),
-                        ips.isEmpty() ? "" : ips,
+                print(format,
+                        StringUtils.substring(endpoints.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        ips.isEmpty() ? "" : StringUtils.substring(ips.toString(),
+                                0, CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH),
                         portWithProtocol.isEmpty() ? "" : portWithProtocol);
             }
         }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sIngressListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sIngressListCommand.java
index 8003e28..3f9dc28 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sIngressListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sIngressListCommand.java
@@ -18,9 +18,11 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.extensions.Ingress;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -30,6 +32,11 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -40,18 +47,19 @@
         description = "Lists all kubernetes ingresses")
 public class K8sIngressListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-15s%-30s";
-
     @Override
     protected void doExecute() {
         K8sIngressService service = get(K8sIngressService.class);
         List<Ingress> ingresses = Lists.newArrayList(service.ingresses());
         ingresses.sort(Comparator.comparing(p -> p.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(ingresses));
         } else {
-            print(FORMAT, "Name", "Namespace", "LB Addresses");
+            print(format, "Name", "Namespace", "LB Addresses");
 
             for (Ingress ingress : ingresses) {
 
@@ -60,9 +68,11 @@
                 ingress.getStatus().getLoadBalancer()
                         .getIngress().forEach(i -> lbIps.add(i.getIp()));
 
-                print(FORMAT,
-                        ingress.getMetadata().getName(),
-                        ingress.getMetadata().getNamespace(),
+                print(format,
+                        StringUtils.substring(ingress.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(ingress.getMetadata().getNamespace(),
+                                0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
                         lbIps.isEmpty() ? "" : lbIps);
             }
         }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java
index 4d69082..db649aa 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java
@@ -18,9 +18,11 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.Namespace;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.k8snetworking.api.K8sNamespaceService;
@@ -29,6 +31,11 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_LABELS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_PHASE_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -38,28 +45,32 @@
         description = "Lists all kubernetes namespaces")
 public class K8sNamespaceListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-15s%-30s";
-
     @Override
     protected void doExecute() {
         K8sNamespaceService service = get(K8sNamespaceService.class);
         List<Namespace> namespaces = Lists.newArrayList(service.namespaces());
         namespaces.sort(Comparator.comparing(n -> n.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_PHASE_LENGTH, CLI_LABELS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(namespaces));
         } else {
-            print(FORMAT, "Name", "Phase", "Labels");
+            print(format, "Name", "Phase", "Labels");
 
             for (Namespace namespace : namespaces) {
 
-                print(FORMAT,
-                        namespace.getMetadata().getName(),
+                print(format,
+                        StringUtils.substring(namespace.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
                         namespace.getStatus().getPhase(),
                         namespace.getMetadata() != null &&
                                 namespace.getMetadata().getLabels() != null &&
                                 !namespace.getMetadata().getLabels().isEmpty() ?
-                                namespace.getMetadata().getLabels() : "");
+                                StringUtils.substring(namespace.getMetadata()
+                                                .getLabels().toString(), 0,
+                                        CLI_LABELS_LENGTH - CLI_MARGIN_LENGTH) : "");
             }
         }
     }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkListCommand.java
index 82904a6..e3562e2 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkListCommand.java
@@ -17,7 +17,9 @@
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -27,6 +29,13 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_ID_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_SEG_ID_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_TYPE_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -37,22 +46,26 @@
         description = "Lists all kubernetes networks")
 public class K8sNetworkListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-40s%-20s%-20s%-20s%-16s";
-
     @Override
     protected void doExecute() {
         K8sNetworkService service = get(K8sNetworkService.class);
         List<K8sNetwork> networks = Lists.newArrayList(service.networks());
         networks.sort(Comparator.comparing(K8sNetwork::name));
 
+        String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH,
+                CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(networks));
         } else {
-            print(FORMAT, "ID", "Name", "Type", "SegId", "Gateway");
+            print(format, "ID", "Name", "Type", "SegId", "Gateway");
 
             for (K8sNetwork net: networks) {
-                print(FORMAT, net.networkId(),
-                        net.name(),
+                print(format,
+                        StringUtils.substring(net.networkId(),
+                                0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(net.name(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
                         net.type().toString(),
                         net.segmentId(),
                         net.gatewayIp() == null ? "" : net.gatewayIp().toString());
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkPolicyListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkPolicyListCommand.java
index cc6d24f..ffc76d7 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkPolicyListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNetworkPolicyListCommand.java
@@ -18,9 +18,11 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.networking.NetworkPolicy;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -30,6 +32,11 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -40,24 +47,27 @@
         description = "Lists all kubernetes network policies")
 public class K8sNetworkPolicyListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-15s%-30s";
-
     @Override
     protected void doExecute() {
         K8sNetworkPolicyService service = get(K8sNetworkPolicyService.class);
         List<NetworkPolicy> policies = Lists.newArrayList(service.networkPolicies());
         policies.sort(Comparator.comparing(p -> p.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH));
+
         if (outputJson()) {
             print("%s", json(policies));
         } else {
-            print(FORMAT, "Name", "Namespace", "Types");
+            print(format, "Name", "Namespace", "Types");
 
             for (NetworkPolicy policy : policies) {
 
-                print(FORMAT,
-                        policy.getMetadata().getName(),
-                        policy.getMetadata().getNamespace(),
+                print(format,
+                        StringUtils.substring(policy.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(policy.getMetadata().getNamespace(),
+                                0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
                         policy.getSpec().getPolicyTypes().isEmpty() ?
                                 "" : policy.getSpec().getPolicyTypes());
             }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPodListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPodListCommand.java
index 7845df1..c597b01 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPodListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPodListCommand.java
@@ -18,9 +18,11 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.Pod;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -30,6 +32,12 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_CONTAINERS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -40,18 +48,19 @@
         description = "Lists all kubernetes pods")
 public class K8sPodListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-15s%-15s%-30s";
-
     @Override
     protected void doExecute() {
         K8sPodService service = get(K8sPodService.class);
         List<Pod> pods = Lists.newArrayList(service.pods());
         pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_CONTAINERS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(pods));
         } else {
-            print(FORMAT, "Name", "Namespace", "IP Address", "Containers");
+            print(format, "Name", "Namespace", "IP Address", "Containers");
 
             for (Pod pod : pods) {
 
@@ -59,10 +68,13 @@
 
                 pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
 
-                print(FORMAT,
-                        pod.getMetadata().getName(),
-                        pod.getMetadata().getNamespace(),
-                        pod.getStatus().getPodIP(),
+                print(format,
+                        StringUtils.substring(pod.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(pod.getMetadata().getNamespace(),
+                                0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(pod.getStatus().getPodIP(),
+                                0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
                         containers.isEmpty() ? "" : containers);
             }
         }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPortListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPortListCommand.java
index e344971..2dabb0e 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPortListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sPortListCommand.java
@@ -18,7 +18,9 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Argument;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
@@ -30,6 +32,12 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_ID_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MAC_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -40,8 +48,6 @@
         description = "Lists all kubernetes ports")
 public class K8sPortListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-40s%-20s%-20s%-8s";
-
     @Argument(name = "networkId", description = "Network ID")
     private String networkId = null;
 
@@ -52,6 +58,9 @@
         List<K8sPort> ports = Lists.newArrayList(service.ports());
         ports.sort(Comparator.comparing(K8sPort::networkId));
 
+        String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH,
+                CLI_NAME_LENGTH, CLI_MAC_ADDRESS_LENGTH, CLI_IP_ADDRESSES_LENGTH));
+
         if (!Strings.isNullOrEmpty(networkId)) {
             ports.removeIf(port -> !port.networkId().equals(networkId));
         }
@@ -59,13 +68,17 @@
         if (outputJson()) {
             print("%s", json(ports));
         } else {
-            print(FORMAT, "ID", "Network", "MAC Address", "Fixed IPs");
+            print(format, "ID", "Network", "MAC Address", "Fixed IPs");
             for (K8sPort port: ports) {
                 K8sNetwork k8sNet = service.network(port.networkId());
-                print(FORMAT, port.portId(),
-                        k8sNet == null ? "" : k8sNet.name(),
-                        port.macAddress(),
-                        port.ipAddress() == null ? "" : port.ipAddress().toString());
+                print(format,
+                        StringUtils.substring(port.portId(),
+                                0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
+                        k8sNet == null ? "" : StringUtils.substring(k8sNet.name(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(port.macAddress().toString(),
+                                0, CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
+                        port.ipAddress() == null ? "" : port.ipAddress());
             }
         }
     }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
index 1d6cf3d..4356bc7 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
@@ -18,8 +18,10 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -29,6 +31,11 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
 
 /**
@@ -39,7 +46,6 @@
         description = "Lists all kubernetes services")
 public class K8sServiceListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-50s%-30s%-30s";
     private static final String PORT_PROTOCOL_SEPARATOR = "/";
 
     @Override
@@ -49,10 +55,13 @@
                 Lists.newArrayList(service.services());
         services.sort(Comparator.comparing(s -> s.getMetadata().getName()));
 
+        String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+                CLI_IP_ADDRESS_LENGTH, CLI_PORTS_LENGTH));
+
         if (outputJson()) {
             print("%s", json(services));
         } else {
-            print(FORMAT, "Name", "Cluster IP", "Ports");
+            print(format, "Name", "Cluster IP", "Ports");
 
             for (io.fabric8.kubernetes.api.model.Service svc : services) {
 
@@ -62,9 +71,11 @@
                         portWithProtocol.add(p.getPort() +
                                 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
 
-                print(FORMAT,
-                        svc.getMetadata().getName(),
-                        svc.getSpec().getClusterIP(),
+                print(format,
+                        StringUtils.substring(svc.getMetadata().getName(),
+                                0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                        StringUtils.substring(svc.getSpec().getClusterIP(),
+                                0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
                         portWithProtocol.isEmpty() ? "" : portWithProtocol);
             }
         }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
index f9bc2c8..a466e14 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.k8snetworking.cli;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.Endpoints;
 import io.fabric8.kubernetes.api.model.Namespace;
@@ -22,6 +23,7 @@
 import io.fabric8.kubernetes.api.model.extensions.Ingress;
 import io.fabric8.kubernetes.api.model.networking.NetworkPolicy;
 import io.fabric8.kubernetes.client.KubernetesClient;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -38,6 +40,17 @@
 
 import java.util.List;
 
+import static org.onosproject.k8snetworking.api.Constants.CLI_CONTAINERS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_LABELS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_PHASE_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH;
+import static org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.syncPortFromPod;
 
 /**
@@ -48,12 +61,19 @@
         description = "Synchronizes all kubernetes states")
 public class K8sSyncStateCommand extends AbstractShellCommand {
 
-    private static final String POD_FORMAT = "%-50s%-15s%-15s%-30s";
-    private static final String SERVICE_FORMAT = "%-50s%-30s%-30s";
-    private static final String ENDPOINTS_FORMAT = "%-50s%-50s%-20s";
-    private static final String INGRESS_FORMAT = "%-50s%-15s%-30s";
-    private static final String NETWORK_POLICY_FORMAT = "%-50s%-15s%-30s";
-    private static final String NAMESPACE_FORMAT = "%-50s%-15s%-30s";
+    private static final String POD_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH,
+            CLI_CONTAINERS_LENGTH));
+    private static final String SERVICE_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_PORTS_LENGTH));
+    private static final String ENDPOINTS_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_IP_ADDRESSES_LENGTH, CLI_PORTS_LENGTH));
+    private static final String INGRESS_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH));
+    private static final String NETWORK_POLICY_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH));
+    private static final String NAMESPACE_FORMAT = genFormatString(ImmutableList.of(
+            CLI_NAME_LENGTH, CLI_PHASE_LENGTH, CLI_LABELS_LENGTH));
 
     @Override
     protected void doExecute() {
@@ -164,8 +184,10 @@
                 .getIngress().forEach(i -> lbIps.add(i.getIp()));
 
         print(INGRESS_FORMAT,
-                ingress.getMetadata().getName(),
-                ingress.getMetadata().getNamespace(),
+                StringUtils.substring(ingress.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                StringUtils.substring(ingress.getMetadata().getNamespace(),
+                        0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
                 lbIps.isEmpty() ? "" : lbIps);
     }
 
@@ -179,19 +201,24 @@
         });
 
         print(ENDPOINTS_FORMAT,
-                endpoints.getMetadata().getName(),
-                ips.isEmpty() ? "" : ips,
+                StringUtils.substring(endpoints.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                ips.isEmpty() ? "" : StringUtils.substring(ips.toString(),
+                        0, CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH),
                 ports.isEmpty() ? "" : ports);
     }
 
     private void printNamespace(Namespace namespace) {
         print(NAMESPACE_FORMAT,
-                namespace.getMetadata().getName(),
+                StringUtils.substring(namespace.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
                 namespace.getStatus().getPhase(),
                 namespace.getMetadata() != null &&
                         namespace.getMetadata().getLabels() != null &&
                         !namespace.getMetadata().getLabels().isEmpty() ?
-                        namespace.getMetadata().getLabels() : "");
+                        StringUtils.substring(namespace.getMetadata()
+                                        .getLabels().toString(), 0,
+                                CLI_LABELS_LENGTH - CLI_MARGIN_LENGTH) : "");
     }
 
     private void printService(io.fabric8.kubernetes.api.model.Service service) {
@@ -201,8 +228,10 @@
         service.getSpec().getPorts().forEach(p -> ports.add(p.getPort()));
 
         print(SERVICE_FORMAT,
-                service.getMetadata().getName(),
-                service.getSpec().getClusterIP(),
+                StringUtils.substring(service.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                StringUtils.substring(service.getSpec().getClusterIP(),
+                        0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
                 ports.isEmpty() ? "" : ports);
     }
 
@@ -213,16 +242,21 @@
         pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
 
         print(POD_FORMAT,
-                pod.getMetadata().getName(),
-                pod.getMetadata().getNamespace(),
-                pod.getStatus().getPodIP(),
+                StringUtils.substring(pod.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                StringUtils.substring(pod.getMetadata().getNamespace(),
+                        0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
+                StringUtils.substring(pod.getStatus().getPodIP(),
+                        0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
                 containers.isEmpty() ? "" : containers);
     }
 
     private void printNetworkPolicy(NetworkPolicy policy) {
         print(NETWORK_POLICY_FORMAT,
-                policy.getMetadata().getName(),
-                policy.getMetadata().getNamespace(),
+                StringUtils.substring(policy.getMetadata().getName(),
+                        0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+                StringUtils.substring(policy.getMetadata().getNamespace(),
+                        0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
                 policy.getSpec().getPolicyTypes().isEmpty() ?
                         "" : policy.getSpec().getPolicyTypes());
     }
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
index 7d20b65..17240fb 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
@@ -55,6 +55,7 @@
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
@@ -546,6 +547,22 @@
         }
     }
 
+    /**
+     * Generates string format based on the given string length list.
+     *
+     * @param stringLengths a list of string lengths
+     * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
+     */
+    public static String genFormatString(List<Integer> stringLengths) {
+        StringBuilder fsb = new StringBuilder();
+        stringLengths.forEach(length -> {
+            fsb.append("%-");
+            fsb.append(length);
+            fsb.append("s");
+        });
+        return fsb.toString();
+    }
+
     private static int binLower(String binStr, int bits) {
         StringBuilder outBin = new StringBuilder(
                 binStr.substring(MASK_BEGIN_IDX, MASK_MAX_IDX - bits));
diff --git a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/cli/K8sNodeListCommand.java b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/cli/K8sNodeListCommand.java
index aea2130..809df39 100644
--- a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/cli/K8sNodeListCommand.java
+++ b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/cli/K8sNodeListCommand.java
@@ -17,7 +17,9 @@
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -27,6 +29,7 @@
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snode.util.K8sNodeUtil.genFormatString;
 import static org.onosproject.k8snode.util.K8sNodeUtil.prettyJson;
 
 /**
@@ -37,7 +40,12 @@
         description = "Lists all nodes registered in kubernetes node service")
 public class K8sNodeListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-28s%-15s%-24s%-20s%-15s";
+    private static final int HOSTNAME_LENGTH = 35;
+    private static final int TYPE_LENGTH = 15;
+    private static final int MANAGEMENT_IP_LENGTH = 25;
+    private static final int DATA_IP_LENGTH = 25;
+    private static final int STATUS = 15;
+    private static final int MARGIN_LENGTH = 2;
 
     @Override
     protected void doExecute() {
@@ -45,16 +53,23 @@
         List<K8sNode> nodes = Lists.newArrayList(nodeService.nodes());
         nodes.sort(Comparator.comparing(K8sNode::hostname));
 
+        String format = genFormatString(ImmutableList.of(HOSTNAME_LENGTH,
+                TYPE_LENGTH, MANAGEMENT_IP_LENGTH, DATA_IP_LENGTH, STATUS));
+
         if (outputJson()) {
             print("%s", json(nodes));
         } else {
-            print(FORMAT, "Hostname", "Type", "Management IP", "Data IP", "State");
+            print(format, "Hostname", "Type", "Management IP", "Data IP", "State");
             for (K8sNode node : nodes) {
-                print(FORMAT,
-                        node.hostname(),
+                print(format,
+                        StringUtils.substring(node.hostname(), 0,
+                                HOSTNAME_LENGTH - MARGIN_LENGTH),
                         node.type(),
-                        node.managementIp(),
-                        node.dataIp() != null ? node.dataIp() : "",
+                        StringUtils.substring(node.managementIp().toString(), 0,
+                                MANAGEMENT_IP_LENGTH - MARGIN_LENGTH),
+                        node.dataIp() != null ? StringUtils.substring(
+                                node.dataIp().toString(), 0,
+                                DATA_IP_LENGTH - MARGIN_LENGTH) : "",
                         node.state());
             }
             print("Total %s nodes", nodeService.nodes().size());
diff --git a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/util/K8sNodeUtil.java b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/util/K8sNodeUtil.java
index 2413f46..023cec9 100644
--- a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/util/K8sNodeUtil.java
+++ b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/util/K8sNodeUtil.java
@@ -37,6 +37,7 @@
 
 import java.io.IOException;
 import java.util.Dictionary;
+import java.util.List;
 
 import static org.onlab.util.Tools.get;
 
@@ -214,6 +215,22 @@
     }
 
     /**
+     * Generates string format based on the given string length list.
+     *
+     * @param stringLengths a list of string lengths
+     * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
+     */
+    public static String genFormatString(List<Integer> stringLengths) {
+        StringBuilder fsb = new StringBuilder();
+        stringLengths.forEach(length -> {
+            fsb.append("%-");
+            fsb.append(length);
+            fsb.append("s");
+        });
+        return fsb.toString();
+    }
+
+    /**
      * Obtains workable kubernetes client.
      *
      * @param config kubernetes API config