Add the logic of generating ovs bridges on bootstrap kubevirt node

Change-Id: Id691738ee31b509a143143103152111dfb47a606
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtCheckNodeCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtCheckNodeCommand.java
new file mode 100644
index 0000000..06297c2
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtCheckNodeCommand.java
@@ -0,0 +1,112 @@
+/*
+ * 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 org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+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.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.device.DeviceService;
+
+import static org.onosproject.kubevirtnode.api.Constants.GENEVE;
+import static org.onosproject.kubevirtnode.api.Constants.GRE;
+import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_BRIDGE;
+import static org.onosproject.kubevirtnode.api.Constants.TUNNEL_BRIDGE;
+import static org.onosproject.kubevirtnode.api.Constants.VXLAN;
+import static org.onosproject.net.AnnotationKeys.PORT_NAME;
+
+/**
+ * Checks detailed node init state.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-check-node",
+        description = "Shows detailed kubevirt nodes status")
+public class KubevirtCheckNodeCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "hostname", description = "Hostname",
+            required = true, multiValued = false)
+    @Completion(KubevirtHostnameCompleter.class)
+    private String hostname = null;
+
+    private static final String MSG_PASS = "PASS";
+    private static final String MSG_FAIL = "FAIL";
+
+    @Override
+    protected void doExecute() throws Exception {
+        KubevirtNodeService nodeService = get(KubevirtNodeService.class);
+        DeviceService deviceService = get(DeviceService.class);
+
+        KubevirtNode node = nodeService.node(hostname);
+        if (node == null) {
+            print("Cannot find %s from registered nodes", hostname);
+            return;
+        }
+
+        print("[Integration Bridge Status]");
+        Device intgBridge = deviceService.getDevice(node.intgBridge());
+        if (intgBridge != null) {
+            print("%s %s=%s available=%s %s",
+                    deviceService.isAvailable(intgBridge.id()) ? MSG_PASS : MSG_FAIL,
+                    INTEGRATION_BRIDGE,
+                    intgBridge.id(),
+                    deviceService.isAvailable(intgBridge.id()),
+                    intgBridge.annotations());
+        }
+
+        print("");
+        print("[Tunnel Bridge Status]");
+        Device tunBridge = deviceService.getDevice(node.tunBridge());
+        if (tunBridge != null) {
+            print("%s %s=%s available=%s %s",
+                    deviceService.isAvailable(tunBridge.id()) ? MSG_PASS : MSG_FAIL,
+                    TUNNEL_BRIDGE,
+                    tunBridge.id(),
+                    deviceService.isAvailable(tunBridge.id()),
+                    tunBridge.annotations());
+
+            if (node.dataIp() != null) {
+                printPortState(deviceService, node.tunBridge(), VXLAN);
+                printPortState(deviceService, node.tunBridge(), GRE);
+                printPortState(deviceService, node.tunBridge(), GENEVE);
+            }
+        }
+    }
+
+    private void printPortState(DeviceService deviceService,
+            DeviceId deviceId, String portName) {
+        Port port = deviceService.getPorts(deviceId).stream()
+                .filter(p -> p.annotations().value(PORT_NAME).equals(portName) &&
+                        p.isEnabled())
+                .findAny().orElse(null);
+
+        if (port != null) {
+            print("%s %s portNum=%s enabled=%s %s",
+                    port.isEnabled() ? MSG_PASS : MSG_FAIL,
+                    portName,
+                    port.number(),
+                    port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
+                    port.annotations());
+        } else {
+            print("%s %s does not exist", MSG_FAIL, portName);
+        }
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtHostnameCompleter.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtHostnameCompleter.java
new file mode 100644
index 0000000..b05ca5f
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtHostnameCompleter.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2020-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 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.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeService;
+
+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 host completer.
+ */
+@Service
+public class KubevirtHostnameCompleter implements Completer {
+    @Override
+    public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        KubevirtNodeService nodeService = get(KubevirtNodeService.class);
+
+        Set<String> hostnames = nodeService.nodes().stream()
+                .map(KubevirtNode::hostname)
+                .collect(Collectors.toSet());
+        SortedSet<String> strings = delegate.getStrings();
+
+        strings.addAll(hostnames);
+
+        return delegate.complete(session, commandLine, candidates);
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtInitNodeCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtInitNodeCommand.java
new file mode 100644
index 0000000..fade52e
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtInitNodeCommand.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2020-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 org.apache.karaf.shell.api.action.Argument;
+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.KubevirtNodeAdminService;
+
+import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
+import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
+
+/**
+ * Initializes nodes for node service.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-init-node",
+        description = "Initializes nodes for KubeVirt node service")
+public class KubevirtInitNodeCommand extends AbstractShellCommand {
+
+    @Option(name = "-a", aliases = "--all", description = "Apply this command to all nodes",
+            required = false, multiValued = false)
+    private boolean isAll = false;
+
+    @Option(name = "-i", aliases = "--incomplete",
+            description = "Apply this command to incomplete nodes",
+            required = false, multiValued = false)
+    private boolean isIncomplete = false;
+
+    @Argument(index = 0, name = "hostnames", description = "Hostname(s) to apply this command",
+            required = false, multiValued = true)
+    @Completion(KubevirtHostnameCompleter.class)
+    private String[] hostnames = null;
+
+    @Override
+    protected void doExecute() throws Exception {
+        KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
+
+        if ((!isAll && !isIncomplete && hostnames == null) ||
+                (isAll && isIncomplete) ||
+                (isIncomplete && hostnames != null) ||
+                (hostnames != null && isAll)) {
+            print("Please specify one of hostname, --all, and --incomplete options.");
+            return;
+        }
+
+        if (isAll) {
+            hostnames = service.nodes().stream()
+                    .map(KubevirtNode::hostname).toArray(String[]::new);
+        } else if (isIncomplete) {
+            hostnames = service.nodes().stream()
+                    .filter(node -> node.state() != COMPLETE)
+                    .map(KubevirtNode::hostname).toArray(String[]::new);
+        }
+
+        for (String hostname : hostnames) {
+            KubevirtNode node = service.node(hostname);
+            if (node == null) {
+                print("Unable to find %s", hostname);
+                continue;
+            }
+            print("Initializing %s", hostname);
+            KubevirtNode updated = node.updateState(INIT);
+            service.updateNode(updated);
+        }
+        print("Done.");
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListApiConfigsCommand.java
similarity index 96%
rename from apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java
rename to apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListApiConfigsCommand.java
index ce64d1e..d01c33a 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListApiConfigsCommand.java
@@ -31,7 +31,7 @@
 @Service
 @Command(scope = "onos", name = "kubevirt-api-configs",
         description = "Lists all KubeVirt API server configs registered to the service")
-public class KubevirtApiConfigListCommand extends AbstractShellCommand {
+public class KubevirtListApiConfigsCommand extends AbstractShellCommand {
 
     private static final String FORMAT = "%-10s%-25s%-10s%-10s";
 
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtNodeListCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListNodesCommand.java
similarity index 97%
rename from apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtNodeListCommand.java
rename to apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListNodesCommand.java
index 8503821..fc4555a 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtNodeListCommand.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtListNodesCommand.java
@@ -38,7 +38,7 @@
 @Service
 @Command(scope = "onos", name = "kubevirt-nodes",
         description = "Lists all nodes registered in KubeVirt node service")
-public class KubevirtNodeListCommand extends AbstractShellCommand {
+public class KubevirtListNodesCommand extends AbstractShellCommand {
 
     private static final int HOSTNAME_LENGTH = 35;
     private static final int TYPE_LENGTH = 15;