[ONOS-5432] Add CLI for refresh/reprogram the data plane for the existing VM.

- Add CLI to purge flow rules installed by openstack apps.
- Add CLI to reinstall flow rules for the existing virtual instances.
- Remove CREATE_TIME from host annotation and revert to use host as a key of security group rule map.

Change-Id: Ie647e5a8c86e86deb8ff050ecf280527ad218eda
diff --git a/apps/openstacknetworking/cli/BUCK b/apps/openstacknetworking/cli/BUCK
new file mode 100644
index 0000000..55a3de2
--- /dev/null
+++ b/apps/openstacknetworking/cli/BUCK
@@ -0,0 +1,13 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:org.apache.karaf.shell.console',
+    '//incubator/api:onos-incubator-api',
+    '//cli:onos-cli',
+    '//utils/osgi:onlab-osgi',
+    '//core/store/serializers:onos-core-serializers',
+    '//apps/openstacknetworking/api:onos-apps-openstacknetworking-api',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
diff --git a/apps/openstacknetworking/cli/pom.xml b/apps/openstacknetworking/cli/pom.xml
new file mode 100644
index 0000000..b5c9ed0
--- /dev/null
+++ b/apps/openstacknetworking/cli/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+~ Copyright 2015-present Open Networking Laboratory
+~
+~ 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.
+-->
+<project
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+    xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-app-openstacknetworking</artifactId>
+        <version>1.8.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-app-openstacknetworking-cli</artifactId>
+    <packaging>bundle</packaging>
+
+    <properties>
+        <api.version>1.0.0</api.version>
+        <api.title>ONOS Openstack Networking REST API</api.title>
+        <api.description>
+            APIs for interacting with Openstack Neutron Plugin.
+        </api.description>
+        <api.package>org.onosproject.openstacknetworking.cli</api.package>
+    </properties>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-openstacknetworking-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-osgi</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstancePurgeFlowsCommand.java b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstancePurgeFlowsCommand.java
new file mode 100644
index 0000000..66f5725
--- /dev/null
+++ b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstancePurgeFlowsCommand.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.openstacknetworking.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostService;
+
+import org.onosproject.openstacknetworking.OpenstackSwitchingService;
+import org.onosproject.openstacknetworking.OpenstackSecurityGroupService;
+import org.onosproject.openstacknetworking.OpenstackRoutingService;
+import org.onosproject.openstacknetworking.OpenstackFloatingIpService;
+
+import static org.onosproject.openstacknetworking.Constants.*;
+
+/**
+ * Purge Flows of OpenstackInstance Data Plane.
+ */
+
+@Command(scope = "onos", name = "openstack-purge-flows",
+        description = "Purge data plane flows of existing VM.")
+public class OpenstackInstancePurgeFlowsCommand extends AbstractShellCommand {
+
+    @Option(name = "-a", aliases = "--all",
+            description = "HostIDs are all existing VM",
+            required = false, multiValued = false)
+    private Boolean allhost = false;
+
+    @Argument(index = 0, name = "hostId", description = "HostID(s)",
+            required = false, multiValued = true)
+    private String[] hostids = null;
+
+    @Override
+    protected void execute() {
+         HostService hostService = AbstractShellCommand.get(HostService.class);
+
+         OpenstackSwitchingService switchingService = getService(OpenstackSwitchingService.class);
+         OpenstackSecurityGroupService sgService = getService(OpenstackSecurityGroupService.class);
+         OpenstackRoutingService routingService = getService(OpenstackRoutingService.class);
+         OpenstackFloatingIpService floatingIpService = getService(OpenstackFloatingIpService.class);
+
+         if (allhost) {
+            switchingService.purgeVmFlow(null);
+            sgService.purgeVmFlow(null);
+            routingService.purgeVmFlow(null);
+            floatingIpService.purgeVmFlow(null);
+
+            hostService.getHosts().forEach(host -> {
+                printHost(host);
+            });
+         } else if (hostids != null) {
+            for (String hostid : hostids) {
+                Host host = hostService.getHost(HostId.hostId(hostid));
+                if (host == null) {
+                    continue;
+                }
+                switchingService.purgeVmFlow(host);
+                sgService.purgeVmFlow(host);
+                routingService.purgeVmFlow(host);
+                floatingIpService.purgeVmFlow(host);
+                printHost(host);
+            }
+         }
+    }
+
+    private void printHost(Host host) {
+        print("Purge data plane flows of VM(hostid=%s, vni=%s, ip=%s, mac=%s).",
+              host.id(), host.annotations().value(VXLAN_ID),
+              host.ipAddresses().stream().findFirst().get().getIp4Address(), host.mac());
+    }
+}
diff --git a/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstanceReInstallFlowCommand.java b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstanceReInstallFlowCommand.java
new file mode 100644
index 0000000..8b53f87
--- /dev/null
+++ b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackInstanceReInstallFlowCommand.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.openstacknetworking.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostService;
+
+import org.onosproject.openstacknetworking.OpenstackSwitchingService;
+import org.onosproject.openstacknetworking.OpenstackSecurityGroupService;
+import org.onosproject.openstacknetworking.OpenstackRoutingService;
+import org.onosproject.openstacknetworking.OpenstackFloatingIpService;
+
+import static org.onosproject.openstacknetworking.Constants.*;
+
+/**
+ * Re-Install Flows of OpenstackInstance Data Plane.
+ */
+
+@Command(scope = "onos", name = "openstack-reinstall-flows",
+        description = "Re-install data plane flows of existing VM.")
+public class OpenstackInstanceReInstallFlowCommand extends AbstractShellCommand {
+
+    @Option(name = "-a", aliases = "--all",
+    description = "HostIDs are all existing VM", required = false, multiValued = false)
+    private Boolean allhost = false;
+
+    @Argument(index = 0, name = "hostId", description = "HostID(s)",
+            required = false, multiValued = true)
+    private String[] hostids = null;
+
+    @Override
+    protected void execute() {
+         HostService hostService = AbstractShellCommand.get(HostService.class);
+
+         OpenstackSwitchingService switchingService = getService(OpenstackSwitchingService.class);
+         OpenstackSecurityGroupService sgService = getService(OpenstackSecurityGroupService.class);
+         OpenstackRoutingService routingService = getService(OpenstackRoutingService.class);
+         OpenstackFloatingIpService floatingIpService = getService(OpenstackFloatingIpService.class);
+
+         if (allhost) {
+            hostService.getHosts().forEach(host -> {
+                switchingService.reinstallVmFlow(host);
+                sgService.reinstallVmFlow(host);
+                routingService.reinstallVmFlow(host);
+                floatingIpService.reinstallVmFlow(host);
+                printHost(host);
+
+            });
+         } else if (hostids != null) {
+            for (String hostid : hostids) {
+                Host host = hostService.getHost(HostId.hostId(hostid));
+                if (host == null) {
+                    continue;
+                }
+                switchingService.reinstallVmFlow(host);
+                sgService.reinstallVmFlow(host);
+                routingService.reinstallVmFlow(host);
+                floatingIpService.reinstallVmFlow(host);
+                printHost(host);
+            }
+         }
+    }
+
+    private void printHost(Host host) {
+        print("Re-install data plane flows of VM(hostid=%s, vni=%s, ip=%s, mac=%s).",
+              host.id(), host.annotations().value(VXLAN_ID),
+              host.ipAddresses().stream().findFirst().get().getIp4Address(), host.mac());
+    }
+}
diff --git a/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/package-info.java b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/package-info.java
new file mode 100644
index 0000000..92d0a2c
--- /dev/null
+++ b/apps/openstacknetworking/cli/src/main/java/org/onosproject/openstacknetworking/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * CLI implementation for refresh/reprogram the data plane for the existing VM(OpenStackInstance).
+ */
+package org.onosproject.openstacknetworking.cli;
diff --git a/apps/openstacknetworking/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/openstacknetworking/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..93076d8
--- /dev/null
+++ b/apps/openstacknetworking/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,25 @@
+<!--
+~ Copyright 2015-present Open Networking Laboratory
+~
+~ 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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+        <command>
+            <action class="org.onosproject.openstacknetworking.cli.OpenstackInstancePurgeFlowsCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.openstacknetworking.cli.OpenstackInstanceReInstallFlowCommand"/>
+        </command>
+    </command-bundle>
+</blueprint>