Bump upstreams
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AbstractPeer.java
index 7893470d4a1c327cfbbfae668ac4ada3b39a5f0a..96ef6019618fc6b59989f499feb138f8b6214200 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.MoreExecutors;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -26,7 +27,6 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteOperations;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
-import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
 import org.opendaylight.protocol.bgp.mode.impl.BGPRouteEntryExportParametersImpl;
 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerTransactionChain;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
@@ -57,6 +57,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -65,7 +66,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImportParameters, Peer,
-        PeerTransactionChain, DOMTransactionChainListener {
+        PeerTransactionChain, FutureCallback<Empty> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractPeer.class);
 
     final RTCClientRouteCache rtCache = new RTCClientRouteCache();
@@ -76,7 +77,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     private final AsNumber localAs;
     private final String name;
 
-    // FIXME: revisit locking here to improve concurrency:
+    // FIXME: Revisit locking here to improve concurrency:
     //        -- identifiers, peerId are a shared resource
     //        -- domChain seems to really be 'ribInChain', accessed from netty thread
     //        -- ribOutChain is accessed from LocRibWriter
@@ -84,6 +85,10 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     //        which needs figuring out.
     @GuardedBy("this")
     private DOMTransactionChain domChain;
+    // FIXME: This is an invariant once the peer is 'resolved' -- which happens instantaneously for ApplicationPeer.
+    //        There are also a number YangInstanceIdentifiers which are tied to it. We want to keep all of them in one
+    //        structure for isolation. This could be a separate DTO (JDK16 record) or isolated into an abstract behavior
+    //        class.
     @GuardedBy("this")
     PeerId peerId;
 
@@ -94,6 +99,8 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     @GuardedBy("this")
     private FluentFuture<? extends CommitInfo> submitted;
 
+    @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
+        justification = "False positive on synchronized createDomChain()")
     AbstractPeer(
             final RIB rib,
             final String peerName,
@@ -107,8 +114,8 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
             final Map<TablesKey, Integer> afiSafisLlGracefulAdvertized) {
         super(rib.getInstanceIdentifier(), groupId, neighborAddress, afiSafisAdvertized, afiSafisGracefulAdvertized,
                 afiSafisLlGracefulAdvertized);
-        this.name = peerName;
-        this.peerRole = role;
+        name = peerName;
+        peerRole = role;
         this.clusterId = clusterId;
         this.localAs = localAs;
         this.rib = rib;
@@ -131,7 +138,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
             return CommitInfo.emptyFluentFuture();
         }
         LOG.info("Closed per Peer {} removed", peerPath);
-        final DOMDataTreeWriteTransaction tx = this.domChain.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction tx = domChain.newWriteOnlyTransaction();
         tx.delete(LogicalDatastoreType.OPERATIONAL, peerPath);
         final FluentFuture<? extends CommitInfo> future = tx.commit();
         future.addCallback(new FutureCallback<CommitInfo>() {
@@ -148,18 +155,18 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         return future;
     }
 
-    synchronized YangInstanceIdentifier createPeerPath() {
-        return this.rib.getYangRibId().node(PEER_NID).node(IdentifierUtils.domPeerId(this.peerId));
+    final YangInstanceIdentifier createPeerPath(final PeerId newPeerId) {
+        return rib.getYangRibId().node(PEER_NID).node(IdentifierUtils.domPeerId(newPeerId));
     }
 
     @Override
     public final synchronized PeerId getPeerId() {
-        return this.peerId;
+        return peerId;
     }
 
     @Override
     public final PeerRole getRole() {
-        return this.peerRole;
+        return peerRole;
     }
 
     @Override
@@ -178,8 +185,8 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     }
 
     @Override
-    public final void onTransactionChainSuccessful(final DOMTransactionChain chain) {
-        LOG.debug("Transaction chain {} successful.", chain);
+    public final void onSuccess(final Empty value) {
+        LOG.debug("Transaction chain successful");
     }
 
     @Override
@@ -199,22 +206,22 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
 
     @Override
     public final String getName() {
-        return this.name;
+        return name;
     }
 
     @Override
     public final ClusterIdentifier getClusterId() {
-        return this.clusterId;
+        return clusterId;
     }
 
     @Override
     public final AsNumber getLocalAs() {
-        return this.localAs;
+        return localAs;
     }
 
     @Override
     public synchronized DOMTransactionChain getDomChain() {
-        return this.domChain;
+        return domChain;
     }
 
     /**
@@ -228,7 +235,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     public final synchronized <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
             void initializeRibOut(final RouteEntryDependenciesContainer entryDep,
                     final List<ActualBestPathRoutes<C, S>> routesToStore) {
-        if (this.ribOutChain == null) {
+        if (ribOutChain == null) {
             LOG.debug("Session closed, skip changes to peer AdjRibsOut {}", getPeerId());
             return;
         }
@@ -237,7 +244,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         final YangInstanceIdentifier tableRibout = getRibOutIId(ribSupport.tablesKey());
         final boolean addPathSupported = supportsAddPathSupported(ribSupport.getTablesKey());
 
-        final DOMDataTreeWriteTransaction tx = this.ribOutChain.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction tx = ribOutChain.newWriteOnlyTransaction();
         for (final ActualBestPathRoutes<C, S> initRoute : routesToStore) {
             if (!supportsLLGR() && initRoute.isDepreferenced()) {
                 // Stale Long-lived Graceful Restart routes should not be propagated
@@ -263,7 +270,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         }
 
         final FluentFuture<? extends CommitInfo> future = tx.commit();
-        this.submitted = future;
+        submitted = future;
         future.addCallback(new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
@@ -281,17 +288,17 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     public final synchronized <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
             void refreshRibOut(final RouteEntryDependenciesContainer entryDep,
                 final List<StaleBestPathRoute> staleRoutes, final List<AdvertizedRoute<C, S>> newRoutes) {
-        if (this.ribOutChain == null) {
+        if (ribOutChain == null) {
             LOG.debug("Session closed, skip changes to peer AdjRibsOut {}", getPeerId());
             return;
         }
-        final DOMDataTreeWriteTransaction tx = this.ribOutChain.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction tx = ribOutChain.newWriteOnlyTransaction();
         final RIBSupport<C, S> ribSupport = entryDep.getRIBSupport();
         deleteRouteRibOut(ribSupport, staleRoutes, tx);
         installRouteRibOut(entryDep, newRoutes, tx);
 
         final FluentFuture<? extends CommitInfo> future = tx.commit();
-        this.submitted = future;
+        submitted = future;
         future.addCallback(new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
@@ -309,7 +316,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
     public final synchronized <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
             void reEvaluateAdvertizement(final RouteEntryDependenciesContainer entryDep,
                 final List<ActualBestPathRoutes<C, S>> routesToStore) {
-        if (this.ribOutChain == null) {
+        if (ribOutChain == null) {
             LOG.debug("Session closed, skip changes to peer AdjRibsOut {}", getPeerId());
             return;
         }
@@ -318,7 +325,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         final NodeIdentifierWithPredicates tk = ribSupport.tablesKey();
         final boolean addPathSupported = supportsAddPathSupported(ribSupport.getTablesKey());
 
-        final DOMDataTreeWriteTransaction tx = this.ribOutChain.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction tx = ribOutChain.newWriteOnlyTransaction();
         for (final ActualBestPathRoutes<C, S> actualBestRoute : routesToStore) {
             final PeerId fromPeerId = actualBestRoute.getFromPeerId();
             if (!filterRoutes(fromPeerId, ribSupport.getTablesKey())) {
@@ -334,7 +341,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
                 final Optional<ContainerNode> effAttr = applyExportPolicy(entryDep, fromPeerId, route, routePath,
                     actualBestRoute.getAttributes());
                 if (effAttr.isPresent()) {
-                    storeRoute(ribSupport, actualBestRoute, route, routePath, effAttr.get(), tx);
+                    storeRoute(ribSupport, actualBestRoute, route, routePath, effAttr.orElseThrow(), tx);
                     continue;
                 }
             }
@@ -343,7 +350,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         }
 
         final FluentFuture<? extends CommitInfo> future = tx.commit();
-        this.submitted = future;
+        submitted = future;
         future.addCallback(new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
@@ -363,7 +370,7 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         final Peer fromPeer = entryDep.getPeerTracker().getPeer(fromPeerId);
         final RIBSupport<?, ?> ribSupport = entryDep.getRIBSupport();
         final BGPRouteEntryExportParameters routeEntry = new BGPRouteEntryExportParametersImpl(fromPeer, this,
-            ribSupport.extractRouteKey(route.getIdentifier()), this.rtCache);
+            ribSupport.extractRouteKey(route.name()), rtCache);
 
         final Attributes bindingAttrs = ribSupport.attributeFromContainerNode(attrs);
         final Optional<Attributes> optExportAttrs = entryDep.getRoutingPolicies().applyExportPolicies(routeEntry,
@@ -475,36 +482,38 @@ abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImp
         tx.delete(LogicalDatastoreType.OPERATIONAL, ribOutTarget);
     }
 
+    // FIXME: make this asynchronous?
     final synchronized void releaseRibOutChain(final boolean isWaitForSubmitted) {
         if (isWaitForSubmitted) {
-            if (this.submitted != null) {
+            if (submitted != null) {
                 try {
-                    this.submitted.get();
+                    submitted.get();
                 } catch (final InterruptedException | ExecutionException throwable) {
                     LOG.error("Write routes failed", throwable);
                 }
             }
         }
 
-        if (this.ribOutChain != null) {
+        if (ribOutChain != null) {
             LOG.info("Closing peer chain {}", getPeerId());
-            this.ribOutChain.close();
-            this.ribOutChain = null;
+            ribOutChain.close();
+            ribOutChain = null;
         }
     }
 
     final synchronized void createDomChain() {
-        if (this.domChain == null) {
+        if (domChain == null) {
             LOG.info("Creating DOM peer chain {}", getPeerId());
-            this.domChain = this.rib.createPeerDOMChain(this);
+            domChain = rib.createPeerDOMChain();
+            domChain.addCallback(this);
         }
     }
 
     final synchronized void closeDomChain() {
-        if (this.domChain != null) {
+        if (domChain != null) {
             LOG.info("Closing DOM peer chain {}", getPeerId());
-            this.domChain.close();
-            this.domChain = null;
+            domChain.close();
+            domChain = null;
         }
     }