Add CLIs to show the detailed info on kubevirt node and pod

Change-Id: I7465e936beec293720bdbcec995b5dad70b4ea78
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtPodCompleter.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtPodCompleter.java
new file mode 100644
index 0000000..4d311c8
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtPodCompleter.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.cli;
+
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodService;
+
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.stream.Collectors;
+
+import static org.onosproject.cli.AbstractShellCommand.get;
+
+/**
+ * Kubevirt POD completer.
+ */
+@Service
+public class KubevirtPodCompleter implements Completer {
+    @Override
+    public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        KubevirtPodService podService = get(KubevirtPodService.class);
+
+        Set<String> podNames = podService.pods().stream()
+                .map(p -> p.getMetadata().getName()).collect(Collectors.toSet());
+        SortedSet<String> strings = delegate.getStrings();
+
+        strings.addAll(podNames);
+
+        return delegate.complete(session, commandLine, candidates);
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java
new file mode 100644
index 0000000..5f7bc4c
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodService;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Show a detailed POD info.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-pod",
+        description = "Displays a POD details")
+public class KubevirtShowPodCommand extends AbstractShellCommand {
+
+    @Option(name = "--name",
+            description = "Filter POD by specific name", multiValued = true)
+    @Completion(KubevirtPodCompleter.class)
+    private List<String> names;
+
+    @Override
+    protected void doExecute() throws Exception {
+        KubevirtPodService service = get(KubevirtPodService.class);
+
+        for (String name : names) {
+            Pod pod = service.pods().stream().filter(p -> p.getMetadata().getName().equals(name))
+                    .findAny().orElse(null);
+            if (pod == null) {
+                print("Unable to find %s", name);
+                continue;
+            }
+
+            if (outputJson()) {
+                print("%s", json(pod));
+            } else {
+                printPod(pod);
+            }
+        }
+    }
+
+    private void printPod(Pod pod) {
+        print("Name: %s", pod.getMetadata().getName());
+        print("  Status: %s", pod.getStatus().getPhase());
+        print("  Node Name: %s", pod.getSpec().getNodeName());
+        print("  Namespace: %s", pod.getMetadata().getNamespace());
+        print("  IP address: %s", pod.getStatus().getPodIP());
+
+        int counter = 1;
+        for (Container container : pod.getSpec().getContainers()) {
+            print("  Container #%d:", counter);
+            print("    Name: %s", container.getName());
+            print("    Image: %s", container.getImage());
+            print("    Pull Policy: %s", container.getImagePullPolicy());
+            print("    Commands: %s", container.getCommand());
+            print("    Args: %s", container.getArgs());
+        }
+    }
+
+    private String json(Pod pod) {
+        try {
+            ObjectNode result = (ObjectNode)
+                    new ObjectMapper().readTree(Serialization.asJson(pod));
+            return prettyJson(new ObjectMapper(), result.toString());
+        } catch (IOException e) {
+            log.warn("Failed to parse POD's JSON string.");
+            return "";
+        }
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java
new file mode 100644
index 0000000..00699a0
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnode.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeService;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+
+import java.util.List;
+
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
+
+/**
+ * Show a node info registered to the service.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-node",
+        description = "Displays a node details")
+public class KubevirtShowNodeCommand extends AbstractShellCommand {
+
+    @Option(name = "--name",
+            description = "Filter kubevirt node by specific name", multiValued = true)
+    @Completion(KubevirtHostnameCompleter.class)
+    private List<String> names;
+
+    @Override
+    protected void doExecute() throws Exception {
+        KubevirtNodeService service = get(KubevirtNodeService.class);
+
+        for (String name : names) {
+            KubevirtNode node = service.node(name);
+            if (node == null) {
+                print("Unable to find %s", name);
+                continue;
+            }
+
+            if (outputJson()) {
+                print("%s", json(node));
+            } else {
+                printNode(node);
+            }
+        }
+    }
+
+    private void printNode(KubevirtNode node) {
+        print("Name: %s", node.hostname());
+        print("  Type: %s", node.type());
+        print("  State: %s", node.state());
+        print("  Management IP: %s", node.managementIp().toString());
+        print("  Data IP: %s", node.dataIp().toString());
+        print("  Integration Bridge: %s", node.intgBridge());
+        print("  Tunneling Bridge: %s", node.tunBridge());
+
+        int counter = 1;
+        for (KubevirtPhyInterface intf: node.phyIntfs()) {
+            print("  Physical interfaces #%d:", counter);
+            print("    Network: %s", intf.network());
+            print("    Interface: %s", intf.intf());
+            counter++;
+        }
+    }
+
+    private String json(KubevirtNode node) {
+        return prettyJson(new ObjectMapper(),
+                jsonForEntity(node, KubevirtNode.class).toString());
+    }
+}