Repository: telstra/open-kilda
Issue: BFD session status is not updated on db

`bfd_session_status` field hasn't been updated in db for isl on any commands.

Changed files:
src-java/network-topology/network-storm-topology/src/test/java/org/openkilda/wfm/topology/network/service/NetworkIslServiceTest.java
src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/DiscoveryBfdMonitor.java
src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/model/IslEndpointBfdStatus.java
src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/IslFsm.java

diff --git a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/DiscoveryBfdMonitor.java b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/DiscoveryBfdMonitor.java
index d0d9cee25..d6093c143 100644
--- a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/DiscoveryBfdMonitor.java
+++ b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/DiscoveryBfdMonitor.java
@@ -72,7 +72,7 @@ public class DiscoveryBfdMonitor extends DiscoveryMonitor<IslEndpointBfdStatus>
                 update = new IslEndpointBfdStatus(true, BfdSessionStatus.DOWN);
                 break;
             case BFD_KILL:
-                update = new IslEndpointBfdStatus(false, null, true);
+                update = new IslEndpointBfdStatus(false, null);
                 break;
             case BFD_FAIL:
                 update = new IslEndpointBfdStatus(false, BfdSessionStatus.FAIL);
@@ -89,10 +89,7 @@ public class DiscoveryBfdMonitor extends DiscoveryMonitor<IslEndpointBfdStatus>
 
     @Override
     public void actualFlush(Endpoint endpoint, Isl persistentView) {
-        IslEndpointBfdStatus bfdStatus = discoveryData.get(endpoint);
-        if (bfdStatus.isForceReset() || bfdStatus.getStatus() != null) {
-            persistentView.setBfdSessionStatus(bfdStatus.getStatus());
-        }
+        persistentView.setBfdSessionStatus(discoveryData.get(endpoint).getStatus());
     }
 
     /**
diff --git a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/IslFsm.java b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/IslFsm.java
index 00843d0de..007d0d5e6 100644
--- a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/IslFsm.java
+++ b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/controller/isl/IslFsm.java
@@ -316,11 +316,17 @@ public final class IslFsm extends AbstractBaseFsm<IslFsm, IslFsmState, IslFsmEve
             for (DiscoveryMonitor<?> entry : monitorsByPriority) {
                 entry.load(start, isl);
             }
+
+            fixUpPersistentData(isl);
         } else {
             log.debug("There is no persistent ISL data {} ==> {} (do not load monitors state)", start, end);
         }
     }
 
+    private void fixUpPersistentData(Isl link) {
+        link.setBfdSessionStatus(null);
+    }
+
     private void sendInstallMultiTable(IIslCarrier carrier) {
         final Endpoint source = reference.getSource();
         final Endpoint dest = reference.getDest();
diff --git a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/model/IslEndpointBfdStatus.java b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/model/IslEndpointBfdStatus.java
index 3d7a12001..50a25460c 100644
--- a/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/model/IslEndpointBfdStatus.java
+++ b/src-java/network-topology/network-storm-topology/src/main/java/org/openkilda/wfm/topology/network/model/IslEndpointBfdStatus.java
@@ -18,22 +18,18 @@ package org.openkilda.wfm.topology.network.model;
 import org.openkilda.model.BfdSessionStatus;
 
 import lombok.AllArgsConstructor;
+import lombok.EqualsAndHashCode;
 import lombok.Value;
 
 @Value
+@EqualsAndHashCode(exclude = {"enabled"})
 @AllArgsConstructor
 public class IslEndpointBfdStatus {
     boolean enabled;
 
     BfdSessionStatus status;
 
-    boolean forceReset;
-
     public IslEndpointBfdStatus() {
         this(false, null);
     }
-
-    public IslEndpointBfdStatus(boolean enabled, BfdSessionStatus status) {
-        this(enabled, status, false);
-    }
 }
diff --git a/src-java/network-topology/network-storm-topology/src/test/java/org/openkilda/wfm/topology/network/service/NetworkIslServiceTest.java b/src-java/network-topology/network-storm-topology/src/test/java/org/openkilda/wfm/topology/network/service/NetworkIslServiceTest.java
index 48182facb..3ba8f3db7 100644
--- a/src-java/network-topology/network-storm-topology/src/test/java/org/openkilda/wfm/topology/network/service/NetworkIslServiceTest.java
+++ b/src-java/network-topology/network-storm-topology/src/test/java/org/openkilda/wfm/topology/network/service/NetworkIslServiceTest.java
@@ -34,6 +34,7 @@ import org.openkilda.config.provider.PropertiesBasedConfigurationProvider;
 import org.openkilda.messaging.command.reroute.RerouteAffectedFlows;
 import org.openkilda.messaging.info.discovery.RemoveIslDefaultRulesResult;
 import org.openkilda.messaging.info.event.IslStatusUpdateNotification;
+import org.openkilda.model.BfdSessionStatus;
 import org.openkilda.model.FeatureToggles;
 import org.openkilda.model.Isl;
 import org.openkilda.model.IslDownReason;
@@ -691,6 +692,52 @@ public class NetworkIslServiceTest {
         verify(dashboardLogger).onIslDown(reference);
     }
 
+    @Test
+    public void resetBfdFailOnStart() {
+        testBfdStatusReset(BfdSessionStatus.FAIL);
+    }
+
+    @Test
+    public void resetBfdUpOnStart() {
+        testBfdStatusReset(BfdSessionStatus.UP);
+    }
+
+    private void testBfdStatusReset(BfdSessionStatus initialStatus) {
+        setupIslStorageStub();
+
+        final Instant start = clock.adjust(Duration.ofSeconds(1));
+        Isl alphaToBeta = makeIsl(endpointAlpha1, endpointBeta2, false)
+                .bfdSessionStatus(initialStatus)
+                .build();
+        islStorage.save(alphaToBeta);
+        islStorage.save(
+                makeIsl(endpointBeta2, endpointAlpha1, false)
+                        .bfdSessionStatus(initialStatus)
+                        .build());
+
+        clock.adjust(Duration.ofSeconds(1));
+
+        IslReference reference = new IslReference(endpointAlpha1, endpointBeta2);
+        service.islSetupFromHistory(reference.getSource(), reference, alphaToBeta);
+
+        Optional<Isl> potential = islStorage.lookup(reference.getSource(), reference.getDest());
+
+        Assert.assertTrue(potential.isPresent());
+        Isl link = potential.get();
+        Assert.assertNull(link.getBfdSessionStatus());
+
+        potential =  islStorage.lookup(reference.getDest(),  reference.getSource());
+        Assert.assertTrue(potential.isPresent());
+        link = potential.get();
+        Assert.assertNull(link.getBfdSessionStatus());
+
+        service.bfdStatusUpdate(reference.getSource(), reference, BfdStatusUpdate.UP);
+        verifyBfdStatus(reference, BfdSessionStatus.UP, null);
+
+        service.bfdStatusUpdate(reference.getDest(), reference, BfdStatusUpdate.UP);
+        verifyBfdStatus(reference, BfdSessionStatus.UP, BfdSessionStatus.UP);
+    }
+
     private IslReference prepareBfdEnabledIsl() {
         IslReference reference = prepareActiveIsl();
 
@@ -748,6 +795,24 @@ public class NetworkIslServiceTest {
         return reference;
     }
 
+    private void verifyBfdStatus(IslReference reference, BfdSessionStatus leftToRight, BfdSessionStatus rightToLeft) {
+        Optional<Isl> potential = islStorage.lookup(reference.getSource(), reference.getDest());
+        Assert.assertTrue(potential.isPresent());
+        verifyBfdStatus(potential.get(), leftToRight);
+
+        potential = islStorage.lookup(reference.getDest(), reference.getSource());
+        Assert.assertTrue(potential.isPresent());
+        verifyBfdStatus(potential.get(), rightToLeft);
+    }
+
+    private void verifyBfdStatus(Isl link, BfdSessionStatus status) {
+        if (status != null) {
+            Assert.assertEquals(status, link.getBfdSessionStatus());
+        } else {
+            Assert.assertNull(link.getBfdSessionStatus());
+        }
+    }
+
     private void verifyIslBandwidthUpdate(long expectedForward, long expectedReverse) {
         Isl forward = lookupIsl(endpointAlpha1, endpointBeta2);
         Assert.assertEquals(expectedForward, forward.getMaxBandwidth());