Repository: telstra/open-kilda
Issue: reroute is not working for a 'vxlan' flow

**Steps to reproduce:**

1. find switch pair with two paths at least;
2. create a vxlan flow;
3. break the flow path(init auto reroute)

**Expected result:**
Flow is rerouted to an alternative path.

**Actual result:**
Flow is not rerouted.
Flow is stayed on the same path and moved to the DOWN status.
No error in kibana.
Error in flowHistory:
```
            {
                "timestamp": 1621864291,
                "action": "Failed to reroute the flow",
                "details": "InstallNonIngressRulesAction failed: No encapsulation resources found for flow path noQINQ_vxlan_r_3_4a5563c7-e35a-436b-aaee-8a452683fe18 (opposite: noQINQ_vxlan_r_3_382162d3-885b-4dda-aec5-ce0369b36fe7)"
            }
```



Changed files:
src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallNonIngressRulesAction.java
src-java/base-topology/base-storm-topology/src/main/java/org/openkilda/wfm/share/service/SpeakerFlowSegmentRequestBuilder.java
src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallIngressRulesAction.java

diff --git a/src-java/base-topology/base-storm-topology/src/main/java/org/openkilda/wfm/share/service/SpeakerFlowSegmentRequestBuilder.java b/src-java/base-topology/base-storm-topology/src/main/java/org/openkilda/wfm/share/service/SpeakerFlowSegmentRequestBuilder.java
index ce72aca06..c204a1a02 100644
--- a/src-java/base-topology/base-storm-topology/src/main/java/org/openkilda/wfm/share/service/SpeakerFlowSegmentRequestBuilder.java
+++ b/src-java/base-topology/base-storm-topology/src/main/java/org/openkilda/wfm/share/service/SpeakerFlowSegmentRequestBuilder.java
@@ -460,7 +460,8 @@ public class SpeakerFlowSegmentRequestBuilder implements FlowCommandBuilder {
         EncapsulationResources resources = resourcesManager
                 .getEncapsulationResources(pathId, oppositePathId, encapsulation)
                 .orElseThrow(() -> new IllegalStateException(format(
-                        "No encapsulation resources found for flow path %s (opposite: %s)", pathId, oppositePathId)));
+                        "No %s encapsulation resources found for flow path %s (opposite: %s).",
+                        encapsulation, pathId, oppositePathId)));
         return new FlowTransitEncapsulation(resources.getTransitEncapsulationId(), resources.getEncapsulationType());
     }
 }
diff --git a/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallIngressRulesAction.java b/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallIngressRulesAction.java
index c887bc704..5edc4cd86 100644
--- a/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallIngressRulesAction.java
+++ b/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallIngressRulesAction.java
@@ -18,7 +18,6 @@ package org.openkilda.wfm.topology.flowhs.fsm.reroute.actions;
 import org.openkilda.floodlight.api.request.FlowSegmentRequest;
 import org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory;
 import org.openkilda.model.Flow;
-import org.openkilda.model.FlowEncapsulationType;
 import org.openkilda.model.FlowPath;
 import org.openkilda.persistence.PersistenceManager;
 import org.openkilda.wfm.share.flow.resources.FlowResourcesManager;
@@ -52,9 +51,14 @@ public class InstallIngressRulesAction extends FlowProcessingAction<FlowRerouteF
         String flowId = stateMachine.getFlowId();
         Flow flow = getFlow(flowId);
 
-        FlowEncapsulationType encapsulationType = stateMachine.getNewEncapsulationType() != null
-                ? stateMachine.getNewEncapsulationType() : flow.getEncapsulationType();
-        FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(encapsulationType);
+        // Detach the entity to avoid propagation to the database.
+        flowRepository.detach(flow);
+        if (stateMachine.getNewEncapsulationType() != null) {
+            // This is for commandBuilder.buildIngressOnly() to use proper (updated) encapsulation type.
+            flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
+        }
+
+        FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
 
         Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();
         if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
diff --git a/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallNonIngressRulesAction.java b/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallNonIngressRulesAction.java
index 25d83bfb0..a3f617c46 100644
--- a/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallNonIngressRulesAction.java
+++ b/src-java/flowhs-topology/flowhs-storm-topology/src/main/java/org/openkilda/wfm/topology/flowhs/fsm/reroute/actions/InstallNonIngressRulesAction.java
@@ -18,7 +18,6 @@ package org.openkilda.wfm.topology.flowhs.fsm.reroute.actions;
 import org.openkilda.floodlight.api.request.FlowSegmentRequest;
 import org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory;
 import org.openkilda.model.Flow;
-import org.openkilda.model.FlowEncapsulationType;
 import org.openkilda.model.FlowPath;
 import org.openkilda.persistence.PersistenceManager;
 import org.openkilda.wfm.share.flow.resources.FlowResourcesManager;
@@ -52,9 +51,14 @@ public class InstallNonIngressRulesAction extends
         String flowId = stateMachine.getFlowId();
         Flow flow = getFlow(flowId);
 
-        FlowEncapsulationType encapsulationType = stateMachine.getNewEncapsulationType() != null
-                ? stateMachine.getNewEncapsulationType() : flow.getEncapsulationType();
-        FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(encapsulationType);
+        // Detach the entity to avoid propagation to the database.
+        flowRepository.detach(flow);
+        if (stateMachine.getNewEncapsulationType() != null) {
+            // This is for commandBuilder.buildAllExceptIngress() to use proper (updated) encapsulation type.
+            flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
+        }
+
+        FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
 
         Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();