Fix: remove network related flow rules on openstack network removal

Change-Id: I3f6501cfd2474665ed244ff10ae604e7aac2f801
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/OpenstackNetworkEvent.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/OpenstackNetworkEvent.java
index 84c9b21..6af8e1d 100644
--- a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/OpenstackNetworkEvent.java
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/OpenstackNetworkEvent.java
@@ -46,6 +46,11 @@
         OPENSTACK_NETWORK_UPDATED,
 
         /**
+         * Signifies that the OpenStack network is pre-removed.
+         */
+        OPENSTACK_NETWORK_PRE_REMOVED,
+
+        /**
          * Signifies that the OpenStack network is removed.
          */
         OPENSTACK_NETWORK_REMOVED,
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/DistributedOpenstackNetworkStore.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/DistributedOpenstackNetworkStore.java
index 2cebd2f..474e6d1 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/DistributedOpenstackNetworkStore.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/DistributedOpenstackNetworkStore.java
@@ -74,6 +74,7 @@
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.EXTERNAL_PEER_ROUTER_REMOVED;
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.EXTERNAL_PEER_ROUTER_UPDATED;
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_CREATED;
+import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_PRE_REMOVED;
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_REMOVED;
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_UPDATED;
 import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_CREATED;
@@ -373,6 +374,20 @@
             notifyDelegate(new OpenstackNetworkEvent(
                     OPENSTACK_NETWORK_UPDATED,
                     event.newValue().value()));
+
+            Network oldValue = event.oldValue().value();
+            Network newValue = event.newValue().value();
+
+            // FIXME: before the network get removed eventually, neutron always
+            // issue network update event with removed (empty) segmentation ID
+            // this might be a bug of openstack or openstack4j, need to revisit later
+            if (oldValue.getProviderSegID() != null &&
+                    newValue.getProviderSegID() == null) {
+                log.debug("OpenStack network pre-removed");
+                notifyDelegate(new OpenstackNetworkEvent(
+                        OPENSTACK_NETWORK_PRE_REMOVED,
+                        event.oldValue().value()));
+            }
         }
 
         private void processNetworkMapInsertion(MapEvent<String, Network> event) {
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingArpHandler.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingArpHandler.java
index 68fe4df..6eb882e 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingArpHandler.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingArpHandler.java
@@ -872,7 +872,7 @@
             Network network = event.subject();
 
             if (network == null) {
-                log.warn("Network is not specified.");
+                log.debug("Network is not specified.");
                 return false;
             } else {
                 return network.getProviderSegID() != null;
@@ -897,9 +897,10 @@
                 case OPENSTACK_NETWORK_UPDATED:
                     eventExecutor.execute(() -> processNetworkCreation(event));
                     break;
-                case OPENSTACK_NETWORK_REMOVED:
+                case OPENSTACK_NETWORK_PRE_REMOVED:
                     eventExecutor.execute(() -> processNetworkRemoval(event));
                     break;
+                case OPENSTACK_NETWORK_REMOVED:
                 case OPENSTACK_PORT_CREATED:
                 case OPENSTACK_PORT_UPDATED:
                 case OPENSTACK_PORT_REMOVED:
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
index c406990..c30ea73 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
@@ -822,7 +822,7 @@
                 case OPENSTACK_NETWORK_UPDATED:
                     eventExecutor.execute(() -> processNetworkAddition(event));
                     break;
-                case OPENSTACK_NETWORK_REMOVED:
+                case OPENSTACK_NETWORK_PRE_REMOVED:
                     eventExecutor.execute(() -> processNetworkRemoval(event));
                     break;
                 case OPENSTACK_PORT_CREATED: