Fix calculation and roll-over of BGP operational neighbor uptime value. 98/74298/1
authorAjay Lele <ajayslele@gmail.com>
Tue, 17 Jul 2018 21:09:14 +0000 (14:09 -0700)
committerAjay Lele <ajayslele@gmail.com>
Fri, 20 Jul 2018 18:09:35 +0000 (11:09 -0700)
Change-Id: I6ba03074658b45c17ee440fa81158720531a2cef
JIRA: BGPCEP-813
Signed-off-by: Ajay Lele <ajayslele@gmail.com>
bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/NeighborUtil.java
bgp/openconfig-state/src/test/java/org/opendaylight/protocol/bgp/state/NeighborUtilTest.java
bgp/openconfig-state/src/test/java/org/opendaylight/protocol/bgp/state/StateProviderImplTest.java

index aee7a33afe1a288ec978bb2e0829b0e22f3265c0..d1694c6a73a622f4c5dc9f72a9ab176f90294732 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.protocol.bgp.state;
 
+import com.google.common.primitives.UnsignedInteger;
 import com.google.common.primitives.UnsignedLong;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -84,6 +85,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.
  * Util for create OpenConfig Neighbor with corresponding openConfig state.
  */
 public final class NeighborUtil {
+    private static final long TIMETICK_ROLLOVER_VALUE = (UnsignedInteger.MAX_VALUE.longValue() + 1);
+
     private NeighborUtil() {
         throw new UnsupportedOperationException();
     }
@@ -162,9 +165,12 @@ public final class NeighborUtil {
         if (neighbor == null) {
             return null;
         }
+        // convert neighbor uptime which is in milliseconds to time-ticks which is
+        // hundredth of a second, and handle roll-over scenario
+        final long uptimeTicks = ((neighbor.getUpTime() / 10) % TIMETICK_ROLLOVER_VALUE);
         final NeighborTimersStateAugmentation timerState = new NeighborTimersStateAugmentationBuilder()
                 .setNegotiatedHoldTime(BigDecimal.valueOf(neighbor.getNegotiatedHoldTime()))
-                .setUptime(new Timeticks(neighbor.getUpTime())).build();
+                .setUptime(new Timeticks(uptimeTicks)).build();
 
         return new TimersBuilder().setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp
                 .neighbor.group.timers.StateBuilder()
index 5f5303d83787d560034edfb4bbfcb28ad82a9e53..d9cb87936c12514d5038c1f29cf5c50193db4b79 100644 (file)
@@ -13,8 +13,10 @@ import static org.junit.Assert.assertNull;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.opendaylight.protocol.bgp.state.StateProviderImplTest.TABLES_KEY;
 
+import java.math.BigDecimal;
 import java.util.Collections;
 import java.util.Optional;
 import org.junit.Before;
@@ -25,19 +27,25 @@ import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer
 import org.opendaylight.protocol.bgp.rib.spi.State;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.GracefulRestart;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.GracefulRestartBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.graceful.restart.StateBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.BgpNeighborState.SessionState;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Timers;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.TimersBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timeticks;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborAfiSafiGracefulRestartStateAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborAfiSafiGracefulRestartStateAugmentationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborAfiSafiStateAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborAfiSafiStateAugmentationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborStateAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborTimersStateAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborTimersStateAugmentationBuilder;
 
 public class NeighborUtilTest {
     @Mock
@@ -65,8 +73,6 @@ public class NeighborUtilTest {
         doReturn(false).when(this.bgpAfiSafiState).isAfiSafiSupported(eq(TABLES_KEY));
         doReturn(false).when(this.bgpAfiSafiState).isGracefulRestartAdvertized(eq(TABLES_KEY));
         doReturn(false).when(this.bgpAfiSafiState).isGracefulRestartReceived(eq(TABLES_KEY));
-
-
     }
 
     @Test
@@ -82,17 +88,47 @@ public class NeighborUtilTest {
     }
 
     @Test
-    public void testBuildTimer() {
+    public void testBuildTimerNullValue() {
         assertNull(NeighborUtil.buildTimer(null));
     }
 
+    @Test
+    public void testBuildTimerNormalValue() {
+        final BGPTimersState timerState = mock(BGPTimersState.class);
+        doReturn(90L).when(timerState).getNegotiatedHoldTime();
+        doReturn(5000L).when(timerState).getUpTime();
+
+        final NeighborTimersStateAugmentation timerStateAug = new NeighborTimersStateAugmentationBuilder()
+                .setNegotiatedHoldTime(BigDecimal.valueOf(90L)).setUptime(new Timeticks(500L)).build();
+        final Timers expectedTimers = new TimersBuilder().setState(
+                new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
+                .StateBuilder().addAugmentation(NeighborTimersStateAugmentation.class, timerStateAug).build())
+                .build();
+        assertEquals(expectedTimers, NeighborUtil.buildTimer(timerState));
+    }
+
+    @Test
+    public void testBuildTimerRollOverValue() {
+        final BGPTimersState timerState = mock(BGPTimersState.class);
+        doReturn(90L).when(timerState).getNegotiatedHoldTime();
+        doReturn(42949673015L).when(timerState).getUpTime();
+
+        final NeighborTimersStateAugmentation timerStateAug = new NeighborTimersStateAugmentationBuilder()
+                .setNegotiatedHoldTime(BigDecimal.valueOf(90L)).setUptime(new Timeticks(5L)).build();
+        final Timers expectedTimers = new TimersBuilder().setState(
+                new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
+                .StateBuilder().addAugmentation(NeighborTimersStateAugmentation.class, timerStateAug).build())
+                .build();
+        assertEquals(expectedTimers, NeighborUtil.buildTimer(timerState));
+    }
+
     @Test
     public void testBuildTransport() {
         assertNull(NeighborUtil.buildTransport(null));
     }
 
     @Test
-    public void testBuildNeighborStatet() {
+    public void testBuildNeighborState() {
         assertNull(NeighborUtil.buildNeighborState(null, null));
     }
 
@@ -119,4 +155,4 @@ public class NeighborUtilTest {
         assertEquals(Collections.singletonList(expected),
                 NeighborUtil.buildAfisSafisState(this.bgpAfiSafiState, this.tableRegistry));
     }
-}
\ No newline at end of file
+}
index 474550c002c482e2099dc3bffe5ca62160ae9a41..24bf23b6ead7f20399e848845fd5d11a7c18f683 100644 (file)
@@ -211,7 +211,7 @@ public class StateProviderImplTest extends AbstractConcurrentDataBrokerTest {
 
         doReturn(this.timersState).when(this.bgpPeerState).getBGPTimersState();
         doReturn(10L).when(this.timersState).getNegotiatedHoldTime();
-        doReturn(1L).when(this.timersState).getUpTime();
+        doReturn(10L).when(this.timersState).getUpTime();
 
         doReturn(this.bgpTransportState).when(this.bgpPeerState).getBGPTransportState();
         doReturn(this.localPort).when(this.bgpTransportState).getLocalPort();