Bug-8942: Fix DelegatedLspsCount 19/61519/5
authorAjay Lele <ajayl.bro@gmail.com>
Thu, 10 Aug 2017 21:49:50 +0000 (14:49 -0700)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Mon, 4 Sep 2017 07:05:07 +0000 (07:05 +0000)
mbean value returns count of all LSPs and not just
the ones that are delegated

- Fixed code to return count of delegated LSPs only
- Added unit-test

Change-Id: I067b4cff8c7503fa878742ab6605fca2685a6c15
Signed-off-by: Ajay Lele <ajayl.bro@gmail.com>
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/AbstractTopologySessionListener.java
pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/Stateful07TopologySessionListenerTest.java

index 4bf4ff6d864d5e4bb963c2009d2067f9855fffd3..e38b7ed6b708f1c6a2677d19ccd9e8d6b2d41f22 100755 (executable)
@@ -20,6 +20,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.TimeUnit;
@@ -39,6 +40,9 @@ import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
 import org.opendaylight.protocol.pcep.TerminationReason;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.LspObject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Path1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MessageHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
@@ -650,8 +654,15 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
     }
 
     @Override
-    public Integer getDelegatedLspsCount() {
-        return this.lsps.size();
+    public synchronized Integer getDelegatedLspsCount() {
+        return Math.toIntExact(this.lspData.values().stream()
+            .map(ReportedLsp::getPath).filter(Objects::nonNull).filter(pathList -> !pathList.isEmpty())
+            // pick the first path, as delegate status should be same in each path
+            .map(pathList -> pathList.get(0))
+            .map(path -> path.getAugmentation(Path1.class)).filter(Objects::nonNull)
+            .map(LspObject::getLsp).filter(Objects::nonNull)
+            .filter(Lsp::isDelegate)
+            .count());
     }
 
     @Override
index e8ec0c24f7112bbdbb2cc732880a2d0b4357e907..c0192b0dc8ab5085d9c28d6972f6e774af3bafcc 100755 (executable)
@@ -587,6 +587,46 @@ public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTe
         });
     }
 
+    @Test
+    public void testDelegatedLspsCountWithDelegation() throws Exception {
+        this.listener.onSessionUp(this.session);
+        this.topologyRpcs.addLsp(createAddLspInput());
+        assertEquals(1, this.receivedMsgs.size());
+        assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
+        final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
+        final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
+        final long srpId = req.getSrp().getOperationId().getValue();
+        final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
+            this.testAddress, this.testAddress, this.testAddress, Optional.absent());
+        //delegate set to true
+        final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
+            .setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active)
+            .setDelegate(true).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(
+                    req.getEro().getSubobject()));
+        this.listener.onMessage(this.session, pcRpt);
+        checkEquals(()->assertEquals(1, this.listener.getDelegatedLspsCount().intValue()));
+    }
+
+    @Test
+    public void testDelegatedLspsCountWithoutDelegation() throws Exception {
+        this.listener.onSessionUp(this.session);
+        this.topologyRpcs.addLsp(createAddLspInput());
+        assertEquals(1, this.receivedMsgs.size());
+        assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
+        final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
+        final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
+        final long srpId = req.getSrp().getOperationId().getValue();
+        final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
+            this.testAddress, this.testAddress, this.testAddress, Optional.absent());
+        //delegate set to false
+        final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
+            .setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active)
+            .setDelegate(false).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(
+                    req.getEro().getSubobject()));
+        this.listener.onMessage(this.session, pcRpt);
+        checkEquals(()->assertEquals(0, this.listener.getDelegatedLspsCount().intValue()));
+    }
+
     @Override
     protected Open getLocalPref() {
         return new OpenBuilder(super.getLocalPref()).setTlvs(new TlvsBuilder().addAugmentation(Tlvs1.class,