Register PCEP session to stats handler only after it is fully initialized
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / AbstractTopologySessionListener.java
index 63ffa1e7edcfcf1d2499b35ddd40a5d752264c9e..86588e0c3196b78bf0c7aad7460bfcaf4f6ba7be 100755 (executable)
@@ -16,6 +16,7 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import io.netty.util.concurrent.FutureListener;
 import java.net.InetAddress;
 import java.util.ArrayList;
@@ -30,7 +31,9 @@ import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
-import javax.annotation.concurrent.GuardedBy;
+import java.util.stream.Stream;
+import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
 import org.opendaylight.bgpcep.pcep.topology.provider.session.stats.SessionStateImpl;
 import org.opendaylight.bgpcep.pcep.topology.provider.session.stats.TopologySessionStats;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
@@ -62,11 +65,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLspKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.reported.lsp.Path;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,10 +83,10 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class AbstractTopologySessionListener<S, L> implements TopologySessionListener, TopologySessionStats {
     static final MessageHeader MESSAGE_HEADER = new MessageHeader() {
-        private final ProtocolVersion version = new ProtocolVersion((short) 1);
+        private final ProtocolVersion version = new ProtocolVersion(Uint8.ONE);
 
         @Override
-        public Class<? extends DataContainer> getImplementedInterface() {
+        public Class<MessageHeader> implementedInterface() {
             return MessageHeader.class;
         }
 
@@ -150,7 +153,6 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
         }
         this.session = psession;
         this.nodeState = state;
-        this.serverSessionManager.bind(this.nodeState.getNodeId(), this.listenerState);
 
         LOG.trace("Peer {} resolved to topology node {}", peerAddress, state.getNodeId());
 
@@ -173,6 +175,7 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
         state.storeNode(topologyAugment,
                 new Node1Builder().setPathComputationClient(pccBuilder.build()).build(), this.session);
         this.listenerState.init(psession);
+        this.serverSessionManager.bind(this.nodeState.getNodeId(), this.listenerState);
         LOG.info("Session with {} attached to topology node {}", psession.getRemoteAddress(), state.getNodeId());
     }
 
@@ -213,7 +216,7 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
      * Tear down the given PCEP session. It's OK to call this method even after the session
      * is already down. It always clear up the current session status.
      */
-    @GuardedBy("this")
+    @Holding("this")
     @SuppressWarnings("checkstyle:IllegalCatch")
     private synchronized void tearDown(final PCEPSession psession) {
 
@@ -437,8 +440,8 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
         this.lspData.put(name, rl);
     }
 
-    private List<Path> makeBeforeBreak(final ReportedLspBuilder rlb, final ReportedLsp previous, final String name,
-            final boolean remove) {
+    private static List<Path> makeBeforeBreak(final ReportedLspBuilder rlb, final ReportedLsp previous,
+            final String name, final boolean remove) {
         // just one path should be reported
         Preconditions.checkState(rlb.getPath().size() == 1);
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId reportedLspId =
@@ -446,7 +449,7 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
         final List<Path> updatedPaths;
         //lspId = 0 and remove = false -> tunnel is down, still exists but no path is signaled
         //remove existing tunnel's paths now, as explicit path remove will not come
-        if (!remove && reportedLspId.getValue() == 0) {
+        if (!remove && reportedLspId.getValue().toJava() == 0) {
             updatedPaths = new ArrayList<>();
             LOG.debug("Remove previous paths {} to this lsp name {}", previous.getPath(), name);
         } else {
@@ -455,7 +458,7 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
             LOG.debug("Found previous paths {} to this lsp name {}", updatedPaths, name);
             for (final Path path : previous.getPath()) {
                 //we found reported path in previous reports
-                if (path.getLspId().getValue() == 0 || path.getLspId().equals(reportedLspId)) {
+                if (path.getLspId().getValue().toJava() == 0 || path.getLspId().equals(reportedLspId)) {
                     LOG.debug("Match on lsp-id {}", path.getLspId().getValue());
                     // path that was reported previously and does have the same lsp-id, path will be updated
                     final boolean r = updatedPaths.remove(path);
@@ -469,7 +472,7 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
         LOG.trace("Adding new path {} to {}", rlb.getPath(), updatedPaths);
         updatedPaths.addAll(rlb.getPath());
         if (remove) {
-            if (reportedLspId.getValue() == 0) {
+            if (reportedLspId.getValue().toJava() == 0) {
                 // if lsp-id also 0, remove all paths
                 LOG.debug("Removing all paths.");
                 updatedPaths.clear();
@@ -594,14 +597,19 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
 
     @Override
     public int getDelegatedLspsCount() {
-        return Math.toIntExact(ImmutableList.copyOf(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.augmentation(Path1.class)).filter(Objects::nonNull)
-                .map(LspObject::getLsp).filter(Objects::nonNull)
-                .filter(Lsp::isDelegate)
-                .count());
+        final Stream<ReportedLsp> stream;
+        synchronized (this) {
+            stream = ImmutableList.copyOf(this.lspData.values()).stream();
+        }
+
+        return Math.toIntExact(stream
+            .map(ReportedLsp::getPath).filter(pathList -> pathList != null && !pathList.isEmpty())
+            // pick the first path, as delegate status should be same in each path
+            .map(pathList -> pathList.get(0))
+            .map(path -> path.augmentation(Path1.class)).filter(Objects::nonNull)
+            .map(LspObject::getLsp).filter(Objects::nonNull)
+            .filter(Lsp::isDelegate)
+            .count());
     }
 
     @Override
@@ -627,6 +635,8 @@ public abstract class AbstractTopologySessionListener<S, L> implements TopologyS
             this.requests.add(req);
         }
 
+        @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+                justification = "https://github.com/spotbugs/spotbugs/issues/811")
         private void notifyRequests() {
             for (final PCEPRequest r : this.requests) {
                 r.done(OperationResults.SUCCESS);