[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/BUCK b/apps/openstacknetworking/BUCK
index 3176898..7af8e0c 100644
--- a/apps/openstacknetworking/BUCK
+++ b/apps/openstacknetworking/BUCK
@@ -1,6 +1,7 @@
 BUNDLES = [
     '//apps/openstacknetworking/api:onos-apps-openstacknetworking-api',
     '//apps/openstacknetworking/web:onos-apps-openstacknetworking-web',
+    '//apps/openstacknetworking/cli:onos-apps-openstacknetworking-cli',
 ]
 
 onos_app (
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackFloatingIpService.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackFloatingIpService.java
index 5f9f6b3..40b2f3f 100644
--- a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackFloatingIpService.java
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackFloatingIpService.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.openstacknetworking;
 
+import org.onosproject.net.Host;
+
 import org.onosproject.openstackinterface.OpenstackFloatingIP;
 
 /**
@@ -47,4 +49,18 @@
      * @param floatingIpId floating ip identifier
      */
     void deleteFloatingIp(String floatingIpId);
+
+    /**
+     * Handles to purge data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void purgeVmFlow(Host host);
+
+    /**
+     * Handles to reinstall data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void reinstallVmFlow(Host host);
 }
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackRoutingService.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackRoutingService.java
index 9923e6f..762f2ed 100644
--- a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackRoutingService.java
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackRoutingService.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.openstacknetworking;
 
+import org.onosproject.net.Host;
+
 import org.onosproject.openstackinterface.OpenstackRouter;
 import org.onosproject.openstackinterface.OpenstackRouterInterface;
 
@@ -60,4 +62,18 @@
      * @param osInterface router interface information
      */
     void removeRouterInterface(OpenstackRouterInterface osInterface);
+
+    /**
+     * Handles to purge data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void purgeVmFlow(Host host);
+
+    /**
+     * Handles to reinstall data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void reinstallVmFlow(Host host);
 }
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSecurityGroupService.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSecurityGroupService.java
index 284a840..c7afbeb 100644
--- a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSecurityGroupService.java
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSecurityGroupService.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.openstacknetworking;
 
+import org.onosproject.net.Host;
+
 import org.onosproject.openstackinterface.OpenstackPort;
 
 /**
@@ -28,4 +30,18 @@
      * @param osPort OpenstackPort information for the VM
      */
     void updateSecurityGroup(OpenstackPort osPort);
+
+    /**
+     * Handles to purge data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void purgeVmFlow(Host host);
+
+    /**
+     * Handles to reinstall data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void reinstallVmFlow(Host host);
 }
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSwitchingService.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSwitchingService.java
new file mode 100644
index 0000000..7032a12
--- /dev/null
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/OpenstackSwitchingService.java
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+import org.onosproject.net.Host;
+
+/**
+ * Handles switch update requests from OpenStack and  CLI.
+ */
+public interface OpenstackSwitchingService {
+    /**
+     * Handles to purge data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void purgeVmFlow(Host host);
+
+    /**
+     * Handles to reinstall data plane flow of existing VM.
+     *
+     * @param host VM Host information
+     */
+    void reinstallVmFlow(Host host);
+}
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>
diff --git a/apps/openstacknetworking/pom.xml b/apps/openstacknetworking/pom.xml
index 4a0b9ef..c2e9706 100644
--- a/apps/openstacknetworking/pom.xml
+++ b/apps/openstacknetworking/pom.xml
@@ -31,6 +31,7 @@
     <modules>
         <module>api</module>
         <module>web</module>
+        <module>cli</module>
         <module>switching</module>
         <module>routing</module>
     </modules>
diff --git a/apps/openstacknetworking/routing/BUCK b/apps/openstacknetworking/routing/BUCK
index fe48a99..7cd9cb1 100644
--- a/apps/openstacknetworking/routing/BUCK
+++ b/apps/openstacknetworking/routing/BUCK
@@ -10,6 +10,7 @@
 BUNDLES = [
     '//apps/openstacknetworking/api:onos-apps-openstacknetworking-api',
     '//apps/openstacknetworking/web:onos-apps-openstacknetworking-web',
+    '//apps/openstacknetworking/cli:onos-apps-openstacknetworking-cli',
     '//apps/openstacknetworking/routing:onos-apps-openstacknetworking-routing',
 ]
 
diff --git a/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackFloatingIpManager.java b/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackFloatingIpManager.java
index bd12b5b..b3990e1 100644
--- a/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackFloatingIpManager.java
+++ b/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackFloatingIpManager.java
@@ -156,6 +156,32 @@
     }
 
     @Override
+    public void reinstallVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                hostDetected(h);
+                log.info("Re-Install data plane flow of virtual machine {}", h);
+            });
+        } else {
+            hostDetected(host);
+            log.info("Re-Install data plane flow of virtual machine {}", host);
+        }
+    }
+
+    @Override
+    public void purgeVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                hostRemoved(h);
+                log.info("Purge data plane flow of virtual machine {}", h);
+            });
+        } else {
+            hostRemoved(host);
+            log.info("Purge data plane flow of virtual machine {}", host);
+        }
+    }
+
+    @Override
     public void createFloatingIp(OpenstackFloatingIP floatingIp) {
     }
 
diff --git a/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackRoutingManager.java b/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackRoutingManager.java
index b331746..c8ce876 100644
--- a/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackRoutingManager.java
+++ b/apps/openstacknetworking/routing/src/main/java/org/onosproject/openstacknetworking/routing/OpenstackRoutingManager.java
@@ -565,6 +565,32 @@
         eventExecutor.execute(() -> removeRoutingRules(host, routableSubNets));
     }
 
+    @Override
+    public void reinstallVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                hostDetected(h);
+                log.info("Re-Install data plane flow of virtual machine {}", h);
+            });
+        } else {
+            hostDetected(host);
+            log.info("Re-Install data plane flow of virtual machine {}", host);
+        }
+    }
+
+    @Override
+    public void purgeVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                hostRemoved(h);
+                log.info("Purge data plane flow of virtual machine {}", h);
+            });
+        } else {
+            hostRemoved(host);
+            log.info("Purge data plane flow of virtual machine {}", host);
+        }
+    }
+
     private class InternalNodeListener implements OpenstackNodeListener {
 
         @Override
diff --git a/apps/openstacknetworking/switching/BUCK b/apps/openstacknetworking/switching/BUCK
index 5e991af..cbb3ec4 100644
--- a/apps/openstacknetworking/switching/BUCK
+++ b/apps/openstacknetworking/switching/BUCK
@@ -10,6 +10,7 @@
 BUNDLES = [
     '//apps/openstacknetworking/api:onos-apps-openstacknetworking-api',
     '//apps/openstacknetworking/web:onos-apps-openstacknetworking-web',
+    '//apps/openstacknetworking/cli:onos-apps-openstacknetworking-cli',
     '//apps/openstacknetworking/switching:onos-apps-openstacknetworking-switching',
 ]
 
diff --git a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSecurityGroupManager.java b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSecurityGroupManager.java
index 9794401..1cc6ef5 100644
--- a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSecurityGroupManager.java
+++ b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSecurityGroupManager.java
@@ -33,7 +33,6 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
-import org.onosproject.net.HostId;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.TrafficSelector;
@@ -79,9 +78,7 @@
     private static final String PROTO_UDP = "UDP";
     private static final String ETHTYPE_IPV4 = "IPV4";
 
-    private final Map<HostId, Set<SecurityGroupRule>> securityGroupRuleMap = Maps.newConcurrentMap();
-    private final Map<HostId, Host> hostInfoMap = Maps.newHashMap();
-
+    private final Map<Host, Set<SecurityGroupRule>> securityGroupRuleMap = Maps.newConcurrentMap();
     private ApplicationId appId;
 
     @Activate
@@ -121,9 +118,9 @@
      */
     private void populateSecurityGroupRules(String tenantId, boolean install) {
         securityGroupRuleMap.entrySet().stream()
-                .filter(entry -> getTenantId(hostInfoMap.get(entry.getKey())).equals(tenantId))
+                .filter(entry -> getTenantId(entry.getKey()).equals(tenantId))
                 .forEach(entry -> {
-                    Host local = hostInfoMap.get(entry.getKey());
+                    Host local = entry.getKey();
                     entry.getValue().forEach(sgRule -> {
                         setSecurityGroupRule(local.location().deviceId(),
                                 sgRule.rule(),
@@ -262,8 +259,7 @@
                 log.warn("Failed to get security group {}", sgId);
             }
         });
-        hostInfoMap.put(host.id(), host);
-        securityGroupRuleMap.put(host.id(), rules);
+        securityGroupRuleMap.put(host, rules);
     }
 
     /**
@@ -297,11 +293,11 @@
     private Set<IpPrefix> getRemoteIps(String tenantId, String sgId) {
         Set<IpPrefix> remoteIps = Sets.newHashSet();
         securityGroupRuleMap.entrySet().stream()
-                .filter(entry -> Objects.equals(getTenantId(hostInfoMap.get(entry.getKey())), tenantId))
+                .filter(entry -> Objects.equals(getTenantId(entry.getKey()), tenantId))
                 .forEach(entry -> {
                     if (entry.getValue().stream()
                             .anyMatch(rule -> rule.rule().secuityGroupId().equals(sgId))) {
-                        remoteIps.add(IpPrefix.valueOf(getIp(hostInfoMap.get(entry.getKey())), 32));
+                        remoteIps.add(IpPrefix.valueOf(getIp(entry.getKey()), 32));
                     }
                 });
         return remoteIps;
@@ -314,8 +310,7 @@
         if (isHostAdded) {
             updateSecurityGroupRulesMap(host);
         } else {
-            securityGroupRuleMap.remove(host.id());
-            hostInfoMap.remove(host.id());
+            securityGroupRuleMap.remove(host);
         }
 
         Tools.stream(hostService.getHosts())
@@ -337,6 +332,59 @@
         log.info("Applied security group rules for {}", host);
     }
 
+    @Override
+    public void reinstallVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                updateSecurityGroupRules(h, true);
+                log.info("Re-Install data plane flow of virtual machine {}", h);
+            });
+        } else {
+            securityGroupRuleMap.entrySet().stream()
+                .filter(entry -> entry.getKey().id().equals(host.id()))
+                .forEach(entry -> {
+                    Host local = entry.getKey();
+                    entry.getValue().forEach(sgRule -> {
+                        setSecurityGroupRule(local.location().deviceId(),
+                                sgRule.rule(),
+                                getIp(local),
+                                sgRule.remoteIp(), true);
+                    });
+                });
+            log.info("Re-Install data plane flow of virtual machine {}", host);
+        }
+    }
+
+    @Override
+    public void purgeVmFlow(Host host) {
+        if (host == null) {
+            securityGroupRuleMap.entrySet().stream()
+                .forEach(entry -> {
+                    Host local = entry.getKey();
+                    entry.getValue().forEach(sgRule -> {
+                        setSecurityGroupRule(local.location().deviceId(),
+                                sgRule.rule(),
+                                getIp(local),
+                                sgRule.remoteIp(), false);
+                    });
+                    log.info("Purge data plane flow of virtual machine {}", local);
+                });
+        } else {
+            securityGroupRuleMap.entrySet().stream()
+                .filter(entry -> entry.getKey().id().equals(host.id()))
+                .forEach(entry -> {
+                    Host local = entry.getKey();
+                    entry.getValue().forEach(sgRule -> {
+                        setSecurityGroupRule(local.location().deviceId(),
+                                sgRule.rule(),
+                                getIp(local),
+                                sgRule.remoteIp(), false);
+                    });
+                });
+            log.info("Purge data plane flow of virtual machine {}", host);
+        }
+    }
+
     private final class SecurityGroupRule {
         private final OpenstackSecurityGroupRule rule;
         private final IpPrefix remoteIp;
@@ -375,4 +423,4 @@
             return Objects.hash(rule, remoteIp);
         }
     }
-}
\ No newline at end of file
+}
diff --git a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingHostManager.java b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingHostManager.java
index 0228af7..8bec3d1 100644
--- a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingHostManager.java
+++ b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingHostManager.java
@@ -175,9 +175,6 @@
         registerDhcpInfo(osPort, openstackSubnet);
         ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number());
 
-
-        // Added CREATE_TIME intentionally to trigger HOST_UPDATED event for the
-        // existing instances.
         DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
                 .set(NETWORK_ID, osPort.networkId())
                 .set(SUBNET_ID, fixedIp.getKey())
@@ -185,8 +182,7 @@
                 .set(VXLAN_ID, osNet.segmentId())
                 .set(TENANT_ID, osNet.tenantId())
                 // TODO remove gateway IP from host annotation
-                .set(GATEWAY_IP, openstackSubnet.gatewayIp())
-                .set(CREATE_TIME, String.valueOf(System.currentTimeMillis()));
+                .set(GATEWAY_IP, openstackSubnet.gatewayIp());
 
         HostDescription hostDesc = new DefaultHostDescription(
                 osPort.macAddress(),
diff --git a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingManager.java b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingManager.java
index 59b41ff..db324d7 100644
--- a/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingManager.java
+++ b/apps/openstacknetworking/switching/src/main/java/org/onosproject/openstacknetworking/switching/OpenstackSwitchingManager.java
@@ -21,6 +21,7 @@
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.IpAddress;
@@ -36,6 +37,7 @@
 import org.onosproject.net.flowobjective.DefaultForwardingObjective;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.openstacknetworking.OpenstackSwitchingService;
 import org.onosproject.openstacknetworking.AbstractVmHandler;
 import org.onosproject.openstacknode.OpenstackNodeService;
 import org.slf4j.Logger;
@@ -47,11 +49,14 @@
 import static org.onosproject.openstacknetworking.Constants.*;
 import static org.onosproject.openstacknetworking.RulePopulatorUtil.buildExtension;
 
+
 /**
  * Populates switching flow rules.
  */
+@Service
 @Component(immediate = true)
-public final class OpenstackSwitchingManager extends AbstractVmHandler {
+public final class OpenstackSwitchingManager extends AbstractVmHandler
+        implements OpenstackSwitchingService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -270,4 +275,30 @@
         removeSwitchingRules(host);
         log.info("Removed virtual machine from switching service {}", host);
     }
+
+    @Override
+    public void reinstallVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                populateSwitchingRules(h);
+                log.info("Re-Install data plane flow of virtual machine {}", h);
+            });
+        } else {
+            populateSwitchingRules(host);
+            log.info("Re-Install data plane flow of virtual machine {}", host);
+        }
+    }
+
+    @Override
+    public void purgeVmFlow(Host host) {
+        if (host == null) {
+            hostService.getHosts().forEach(h -> {
+                removeSwitchingRules(h);
+                log.info("Purge data plane flow of virtual machine {}", h);
+            });
+        } else {
+            removeSwitchingRules(host);
+            log.info("Purge data plane flow of virtual machine {}", host);
+        }
+    }
 }