Clean up SyncOptimization 47/101947/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Aug 2022 20:26:17 +0000 (22:26 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Aug 2022 20:42:33 +0000 (22:42 +0200)
SyncOptimization is a pure DTO, which is immutable and does not directly
interact with PCEPSession.

Change-Id: I73a2f503d925f618c04c204477531ac11cfc2b06
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/topology/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractTopologySessionListener.java
pcep/topology/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/SyncOptimization.java
pcep/topology/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/SyncOptimizationTest.java

index dd66889b14ea9bea8f9ade79dfe4260733cb9af4..5bfb71a6e9af9e33a36bae4270ab9b644d6ce0eb 100644 (file)
@@ -131,7 +131,7 @@ public abstract class AbstractTopologySessionListener implements TopologySession
                  */
                 final InetAddress peerAddress = psession.getRemoteAddress();
 
-                syncOptimization = new SyncOptimization(psession);
+                syncOptimization = new SyncOptimization(psession.getLocalTlvs(), psession.getRemoteTlvs());
                 final boolean haveLspDbVersion = syncOptimization.isDbVersionPresent();
 
                 final TopologyNodeState state =
index 6f2071d01545d81fc85710067691106206983a7a..f9d67ff26cf797eded1c90e7350a4ccaaaa3c088 100644 (file)
@@ -5,20 +5,17 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.bgpcep.pcep.topology.provider;
 
-import static java.util.Objects.requireNonNull;
-
-import org.opendaylight.protocol.pcep.PCEPSession;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Stateful1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.lsp.db.version.tlv.LspDbVersion;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Tlvs1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
+import org.opendaylight.yangtools.concepts.Immutable;
 
-final class SyncOptimization {
-
+final class SyncOptimization implements Immutable {
     private final boolean dbVersionMatch;
     private final boolean isSyncAvoidanceEnabled;
     private final boolean isDeltaSyncEnabled;
@@ -26,90 +23,84 @@ final class SyncOptimization {
     private final boolean isTriggeredInitialSyncEnable;
     private final boolean isTriggeredReSyncEnable;
 
-    SyncOptimization(final PCEPSession session) {
-        requireNonNull(session);
-        final Tlvs remote = session.getRemoteTlvs();
-        final Tlvs local = session.getLocalTlvs();
-        final LspDbVersion localLspDbVersion = getLspDbVersion(local);
-        final LspDbVersion remoteLspDbVersion = getLspDbVersion(remote);
-        this.dbVersionMatch = compareLspDbVersion(localLspDbVersion, remoteLspDbVersion);
-        this.isSyncAvoidanceEnabled = isSyncAvoidance(local) && isSyncAvoidance(remote);
-        this.isDeltaSyncEnabled = isDeltaSync(local) && isDeltaSync(remote);
-        this.isDbVersionPresent = localLspDbVersion != null || remoteLspDbVersion != null;
-        this.isTriggeredInitialSyncEnable = isTriggeredInitialSync(local) && isTriggeredInitialSync(remote)
-                && (this.isDeltaSyncEnabled || this.isSyncAvoidanceEnabled);
-        this.isTriggeredReSyncEnable = isTriggeredReSync(local) && isTriggeredReSync(remote);
+    SyncOptimization(final Tlvs local, final Tlvs remote) {
+        final var localLspDbVersion = lspDbVersion(local);
+        final var remoteLspDbVersion = lspDbVersion(remote);
+        dbVersionMatch = localLspDbVersion != null && localLspDbVersion.equals(remoteLspDbVersion);
+        isDbVersionPresent = localLspDbVersion != null || remoteLspDbVersion != null;
+
+        final var localStateful = stateful(local);
+        final var removeStateful = stateful(remote);
+        isSyncAvoidanceEnabled = isSyncAvoidance(localStateful) && isSyncAvoidance(removeStateful);
+        isDeltaSyncEnabled = isDeltaSync(localStateful) && isDeltaSync(removeStateful);
+        isTriggeredInitialSyncEnable = isTriggeredInitialSync(localStateful) && isTriggeredInitialSync(removeStateful)
+                && (isDeltaSyncEnabled || isSyncAvoidanceEnabled);
+        isTriggeredReSyncEnable = isTriggeredReSync(localStateful) && isTriggeredReSync(removeStateful);
     }
 
     boolean doesLspDbMatch() {
-        return this.dbVersionMatch;
+        return dbVersionMatch;
     }
 
     boolean isSyncAvoidanceEnabled() {
-        return this.isSyncAvoidanceEnabled;
+        return isSyncAvoidanceEnabled;
     }
 
     boolean isDeltaSyncEnabled() {
-        return this.isDeltaSyncEnabled;
+        return isDeltaSyncEnabled;
     }
 
     boolean isTriggeredInitSyncEnabled() {
-        return this.isTriggeredInitialSyncEnable;
+        return isTriggeredInitialSyncEnable;
     }
 
     boolean isTriggeredReSyncEnabled() {
-        return this.isTriggeredReSyncEnable;
+        return isTriggeredReSyncEnable;
     }
 
     boolean isDbVersionPresent() {
-        return this.isDbVersionPresent;
+        return isDbVersionPresent;
     }
 
-    private static LspDbVersion getLspDbVersion(final Tlvs openTlvs) {
+    private static @Nullable LspDbVersion lspDbVersion(final Tlvs openTlvs) {
         if (openTlvs != null) {
-            final Tlvs3 tlvs3 = openTlvs.augmentation(Tlvs3.class);
-            if (tlvs3 != null && tlvs3.getLspDbVersion() != null
-                    && tlvs3.getLspDbVersion().getLspDbVersionValue() != null) {
-                return tlvs3.getLspDbVersion();
+            final var tlvs3 = openTlvs.augmentation(Tlvs3.class);
+            if (tlvs3 != null) {
+                final var dbVersion = tlvs3.getLspDbVersion();
+                if (dbVersion != null && dbVersion.getLspDbVersionValue() != null) {
+                    return dbVersion;
+                }
             }
         }
         return null;
     }
 
-    private static boolean compareLspDbVersion(final LspDbVersion local, final LspDbVersion remote) {
-        if (local != null && remote != null) {
-            return local.equals(remote);
-        }
-        return false;
-    }
-
-    private static Stateful1 getStateful1(final Tlvs openTlvs) {
+    private static @Nullable Stateful1 stateful(final Tlvs openTlvs) {
         if (openTlvs != null) {
-            final Tlvs1 tlvs1 = openTlvs.augmentation(Tlvs1.class);
-            if (tlvs1 != null && tlvs1.getStateful() != null) {
-                return tlvs1.getStateful().augmentation(Stateful1.class);
+            final var tlvs1 = openTlvs.augmentation(Tlvs1.class);
+            if (tlvs1 != null) {
+                final var stateful = tlvs1.getStateful();
+                if (stateful != null) {
+                    return stateful.augmentation(Stateful1.class);
+                }
             }
         }
         return null;
     }
 
-    private static boolean isSyncAvoidance(final Tlvs openTlvs) {
-        final Stateful1 stateful1 = getStateful1(openTlvs);
-        return stateful1 != null && Boolean.TRUE.equals(stateful1.getIncludeDbVersion());
+    private static boolean isSyncAvoidance(final Stateful1 stateful) {
+        return stateful != null && Boolean.TRUE.equals(stateful.getIncludeDbVersion());
     }
 
-    private static boolean isDeltaSync(final Tlvs openTlvs) {
-        final Stateful1 stateful1 = getStateful1(openTlvs);
-        return stateful1 != null && Boolean.TRUE.equals(stateful1.getDeltaLspSyncCapability());
+    private static boolean isDeltaSync(final Stateful1 stateful) {
+        return stateful != null && Boolean.TRUE.equals(stateful.getDeltaLspSyncCapability());
     }
 
-    private static boolean isTriggeredInitialSync(final Tlvs openTlvs) {
-        final Stateful1 stateful1 = getStateful1(openTlvs);
-        return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredInitialSync());
+    private static boolean isTriggeredInitialSync(final Stateful1 stateful) {
+        return stateful != null && Boolean.TRUE.equals(stateful.getTriggeredInitialSync());
     }
 
-    private static boolean isTriggeredReSync(final Tlvs openTlvs) {
-        final Stateful1 stateful1 = getStateful1(openTlvs);
-        return stateful1 != null && Boolean.TRUE.equals(stateful1.getTriggeredResync());
+    private static boolean isTriggeredReSync(final Stateful1 stateful) {
+        return stateful != null && Boolean.TRUE.equals(stateful.getTriggeredResync());
     }
 }
index 356c4c90d269ffe4560b46dd5f2cc280120cdcd4..2f6bf82e19a84e6f91e458eb0a597b4dfe340672 100644 (file)
@@ -9,13 +9,8 @@ package org.opendaylight.bgpcep.pcep.topology.provider;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
 
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Stateful1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.lsp.db.version.tlv.LspDbVersionBuilder;
@@ -25,85 +20,52 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
 import org.opendaylight.yangtools.yang.common.Uint64;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class SyncOptimizationTest {
-    @Mock
-    private PCEPSession pcepSession;
-
     @Test
     public void testDoesLspDbMatchPositive() {
-        final Tlvs tlvs = createTlvs(1L, false, false);
-        doReturn(tlvs).when(pcepSession).getLocalTlvs();
-        doReturn(tlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertTrue(syncOpt.doesLspDbMatch());
+        final var tlvs = createTlvs(1L, false, false);
+        assertTrue(new SyncOptimization(tlvs, tlvs).doesLspDbMatch());
     }
 
     @Test
     public void testDoesLspDbMatchNegative() {
-        final Tlvs localTlvs = createTlvs(1L, false, false);
-        final Tlvs remoteTlvs = createTlvs(2L, false, false);
-        doReturn(localTlvs).when(pcepSession).getLocalTlvs();
-        doReturn(remoteTlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertFalse(syncOpt.doesLspDbMatch());
+        assertFalse(new SyncOptimization(createTlvs(1L, false, false), createTlvs(2L, false, false)).doesLspDbMatch());
     }
 
     @Test
     public void testIsSyncAvoidanceEnabledPositive() {
-        final Tlvs tlvs = createTlvs(1L, true, false);
-        doReturn(tlvs).when(pcepSession).getLocalTlvs();
-        doReturn(tlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertTrue(syncOpt.isSyncAvoidanceEnabled());
+        final var tlvs = createTlvs(1L, true, false);
+        assertTrue(new SyncOptimization(tlvs, tlvs).isSyncAvoidanceEnabled());
     }
 
     @Test
     public void testIsSyncAvoidanceEnabledNegative() {
-        final Tlvs localTlvs = createTlvs(1L, true, false);
-        final Tlvs remoteTlvs = createTlvs(2L, false, false);
-        doReturn(localTlvs).when(pcepSession).getLocalTlvs();
-        doReturn(remoteTlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertFalse(syncOpt.isSyncAvoidanceEnabled());
+        assertFalse(new SyncOptimization(createTlvs(1L, true, false), createTlvs(2L, false, false))
+            .isSyncAvoidanceEnabled());
     }
 
     @Test
     public void testIsDeltaSyncEnabledPositive() {
-        final Tlvs tlvs = createTlvs(1L, true, true);
-        doReturn(tlvs).when(pcepSession).getLocalTlvs();
-        doReturn(tlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertTrue(syncOpt.isDeltaSyncEnabled());
+        final var tlvs = createTlvs(1L, true, true);
+        assertTrue(new SyncOptimization(tlvs, tlvs).isDeltaSyncEnabled());
     }
 
     @Test
     public void testIsDeltaSyncEnabledNegative() {
-        final Tlvs localTlvs = createTlvs(1L, true, true);
-        final Tlvs remoteTlvs = createTlvs(2L, false, false);
-        doReturn(localTlvs).when(pcepSession).getLocalTlvs();
-        doReturn(remoteTlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertFalse(syncOpt.isDeltaSyncEnabled());
+        assertFalse(new SyncOptimization(createTlvs(1L, true, true), createTlvs(2L, false, false))
+            .isDeltaSyncEnabled());
     }
 
     @Test
     public void testIsDbVersionPresentPositive() {
-        final Tlvs localTlvs = createTlvs(null, false, false);
-        final Tlvs remoteTlvs = createTlvs(2L, false, false);
-        doReturn(localTlvs).when(pcepSession).getLocalTlvs();
-        doReturn(remoteTlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertTrue(syncOpt.isDbVersionPresent());
+        assertTrue(new SyncOptimization(createTlvs(null, false, false), createTlvs(2L, false, false))
+            .isDbVersionPresent());
     }
 
     @Test
     public void testIsDbVersionPresentNegative() {
-        final Tlvs tlvs = createTlvs(null, true, false);
-        doReturn(tlvs).when(pcepSession).getLocalTlvs();
-        doReturn(tlvs).when(pcepSession).getRemoteTlvs();
-        final SyncOptimization syncOpt = new SyncOptimization(pcepSession);
-        assertFalse(syncOpt.isDbVersionPresent());
+        final var tlvs = createTlvs(null, true, false);
+        assertFalse(new SyncOptimization(tlvs, tlvs).isDbVersionPresent());
     }
 
     private static Tlvs createTlvs(final Long lspDbVersion, final boolean includeDbVresion,