Purge logic of waiting patch port creation with minor refactoring

Change-Id: Ia9179941cac5ab2cc440176ec02b056efcacccb0
(cherry picked from commit 69985803f87f429302c3f10f05f5004e2ac4a9b9)
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtNetworkHandler.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtNetworkHandler.java
index e604917..0408111 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtNetworkHandler.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtNetworkHandler.java
@@ -432,6 +432,20 @@
         // security group related rules
         setTenantEgressTransitionRule(network.tenantDeviceId(node.hostname()), true);
 
+        // if patch port is available, we install ingress transition rule;
+        // otherwise the patch port event will trigger the rule installation
+        boolean installed = false;
+        for (Port port : deviceService.getPorts(deviceId)) {
+            String portName = port.annotations().value(PORT_NAME);
+            if (StringUtils.startsWithIgnoreCase(portName, TENANT_TO_TUNNEL_PREFIX)) {
+                setTenantIngressTransitionRule(deviceId, port, true);
+                installed = true;
+            }
+        }
+        if (!installed) {
+            log.warn("Installing ingress transition rule failed on tenant bridge {}", network.tenantBridgeName());
+        }
+
         log.info("Install default flow rules for tenant bridge {}", network.tenantBridgeName());
     }
 
@@ -1329,19 +1343,6 @@
                     setElbInternalIpArpResponseRules(node, true);
                 }
             }
-
-            for (KubevirtNetwork network : networkService.networks()) {
-                DeviceId deviceId = network.tenantDeviceId(node.hostname());
-                if (deviceId == null) {
-                    continue;
-                }
-                for (Port port : deviceService.getPorts(deviceId)) {
-                    String portName = port.annotations().value(PORT_NAME);
-                    if (StringUtils.startsWithIgnoreCase(portName, TENANT_TO_TUNNEL_PREFIX)) {
-                        installTenantIngressTransitionRule(deviceId, port, true);
-                    }
-                }
-            }
         }
 
         private void processNodeDeletion(KubevirtNode node) {
@@ -1420,7 +1421,7 @@
         }
     }
 
-    private void installTenantIngressTransitionRule(DeviceId deviceId, Port port, boolean install) {
+    private void setTenantIngressTransitionRule(DeviceId deviceId, Port port, boolean install) {
 
         String portName = port.annotations().value(PORT_NAME);
         if (!StringUtils.startsWithIgnoreCase(portName, TENANT_TO_TUNNEL_PREFIX)) {
@@ -1466,7 +1467,7 @@
                         if (!isRelevantHelper()) {
                             return;
                         }
-                        installTenantIngressTransitionRule(device.id(), port, true);
+                        setTenantIngressTransitionRule(device.id(), port, true);
                     });
                     break;
                 case PORT_REMOVED:
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingTenantHandler.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingTenantHandler.java
index 010f675..0856117 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingTenantHandler.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingTenantHandler.java
@@ -70,7 +70,6 @@
 import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VLAN;
 import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.tunnelPort;
 import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.tunnelToTenantPort;
-import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.waitFor;
 import static org.onosproject.kubevirtnetworking.util.RulePopulatorUtil.buildExtension;
 import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.MASTER;
 import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER;
@@ -150,12 +149,21 @@
                 return;
             }
 
-            KubevirtNode updatedNode = kubevirtNodeService.node(node.hostname());
-            if (tunnelToTenantPort(deviceService, updatedNode, network) == null) {
-                continue;
+            if (network.type() == FLAT || network.type() == VLAN) {
+                return;
             }
 
-            PortNumber patchPortNumber = tunnelToTenantPort(deviceService, node, network);
+            if (network.segmentId() == null) {
+                return;
+            }
+
+            KubevirtNode updatedNode = kubevirtNodeService.node(node.hostname());
+            PortNumber patchPortNumber = tunnelToTenantPort(deviceService, updatedNode, network);
+            if (patchPortNumber == null) {
+                log.warn("Patch port of tenant {} is not ready for node {}",
+                        network.segmentId(), updatedNode.hostname());
+                continue;
+            }
 
             TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
                     .matchTunnelId(Long.parseLong(network.segmentId()));
@@ -206,22 +214,18 @@
                 continue;
             }
 
-            while (true) {
-                KubevirtNode updatedNode = kubevirtNodeService.node(localNode.hostname());
-                if (tunnelToTenantPort(deviceService, updatedNode, network) != null) {
-                    break;
-                } else {
-                    log.info("Waiting for tunnel to tenant patch port creation " +
-                             "on egress rule setup on node {}", updatedNode);
-                    waitFor(3);
-                }
-            }
-
             PortNumber patchPortNumber = tunnelToTenantPort(deviceService, remoteNode, network);
+            if (patchPortNumber == null) {
+                log.warn("Patch port of tenant {} is not ready for node {}",
+                        network.segmentId(), remoteNode.hostname());
+                continue;
+            }
 
             PortNumber tunnelPortNumber = tunnelPort(remoteNode, network);
             if (tunnelPortNumber == null) {
-                return;
+                log.warn("Tunnel port of tenant {} is not ready for node {}",
+                        network.segmentId(), remoteNode.hostname());
+                continue;
             }
 
             TrafficSelector.Builder sIpBuilder = DefaultTrafficSelector.builder()