*/
package org.opendaylight.bgpcep.pcep.topology.provider;
+import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.FluentFuture;
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 io.netty.util.concurrent.FutureListener;
+import io.netty.util.concurrent.Future;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
-import java.util.Timer;
-import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.checkerframework.checker.lock.qual.GuardedBy;
import org.checkerframework.checker.lock.qual.Holding;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
-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;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.LspId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.Node1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.Node1Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.OperationResult;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.PccSyncState;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TearDownSessionInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.lsp.metadata.Metadata;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.PathComputationClient;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.PathComputationClientBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.ReportedLsp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.ReportedLspKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.reported.lsp.Path;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.path.computation.client.reported.lsp.PathKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.LspId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.Node1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.Node1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.OperationResult;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.PccSyncState;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.TearDownSessionInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.lsp.metadata.Metadata;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.PathComputationClient;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.PathComputationClientBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.ReportedLsp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.ReportedLspKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.StatefulTlvBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.reported.lsp.Path;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.path.computation.client.reported.lsp.PathKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
@GuardedBy("this")
final Map<PlspId, String> lsps = new HashMap<>();
@GuardedBy("this")
- SessionStateImpl listenerState;
+ private ObjectRegistration<SessionStateUpdater> listenerState;
+ // FIXME: clarify lifecycle rules of this map, most notably the interaction of multiple SrpIdNumbers
@GuardedBy("this")
private final Map<SrpIdNumber, PCEPRequest> requests = new HashMap<>();
@GuardedBy("this")
private final Map<String, ReportedLsp> lspData = new ConcurrentHashMap<>();
private final ServerSessionManager serverSessionManager;
+ private final SessionStateRegistry stateRegistry;
+
private InstanceIdentifier<PathComputationClient> pccIdentifier;
@GuardedBy("this")
private TopologyNodeState nodeState;
@GuardedBy("this")
private boolean triggeredResyncInProcess;
- AbstractTopologySessionListener(final ServerSessionManager serverSessionManager) {
+ AbstractTopologySessionListener(final SessionStateRegistry stateRegistry,
+ final ServerSessionManager serverSessionManager) {
+ this.stateRegistry = requireNonNull(stateRegistry);
this.serverSessionManager = requireNonNull(serverSessionManager);
}
*/
final InetAddress peerAddress = psession.getRemoteAddress();
- syncOptimization = new SyncOptimization(psession);
- final boolean haveLspDbVersion = syncOptimization.isDbVersionPresent();
+ syncOptimization = SyncOptimization.of(psession.getLocalTlvs(), psession.getRemoteTlvs());
+ final boolean haveLspDbVersion = syncOptimization.dbVersionPresent();
final TopologyNodeState state =
serverSessionManager.takeNodeState(peerAddress, this, haveLspDbVersion);
session = psession;
nodeState = state;
- LOG.trace("Peer {} resolved to topology node {}", peerAddress, state.getNodeId());
+ final var nodeId = state.getNodeId();
+ LOG.trace("Peer {} resolved to topology node {}", peerAddress, nodeId);
// Our augmentation in the topology node
final PathComputationClientBuilder pccBuilder = new PathComputationClientBuilder()
synced.set(isSynchronized());
- final InstanceIdentifier<Node1> topologyAugment = state.getNodeId().augmentation(Node1.class);
+ final InstanceIdentifier<Node1> topologyAugment = nodeId.augmentation(Node1.class);
pccIdentifier = topologyAugment.child(PathComputationClient.class);
if (haveLspDbVersion) {
initialNodeState.augmentation(Node1.class).getPathComputationClient().getReportedLsp());
}
}
- state.storeNode(topologyAugment,
- new Node1Builder().setPathComputationClient(pccBuilder.build()).build(), psession);
- listenerState = new SessionStateImpl(this, psession);
- serverSessionManager.bind(state.getNodeId(), listenerState);
- LOG.info("Session with {} attached to topology node {}", peerAddress, state.getNodeId());
+ final var storeFuture = state.storeNode(topologyAugment,
+ new Node1Builder().setPathComputationClient(pccBuilder.build()).build());
+ listenerState = stateRegistry.bind(new SessionStateUpdater(this, psession, state));
+ LOG.info("Session with {} attached to topology node {}", peerAddress, nodeId);
+
+ storeFuture.addCallback(new FutureCallback<CommitInfo>() {
+ @Override
+ public void onSuccess(final CommitInfo result) {
+ LOG.trace("Node stored {} for session {} updated successfully", topologyAugment, psession);
+ }
+
+ @Override
+ public void onFailure(final Throwable cause) {
+ LOG.error("Failed to store node {} for session {}, terminating it", topologyAugment, psession,
+ cause);
+ session.close(TerminationReason.UNKNOWN);
+ }
+ }, MoreExecutors.directExecutor());
}
}
}
LOG.error("Session {} cannot be closed.", psession, e);
}
session = null;
- listenerState = null;
syncOptimization = null;
-
- // Clear all requests we know about
- for (final Entry<SrpIdNumber, PCEPRequest> e : requests.entrySet()) {
- final PCEPRequest r = e.getValue();
- switch (r.getState()) {
- case DONE:
- // Done is done, nothing to do
- LOG.trace("Request {} was done when session went down.", e.getKey());
- break;
- case UNACKED:
- // Peer has not acked: results in failure
- LOG.info("Request {} was incomplete when session went down, failing the instruction",
- e.getKey());
- r.done(OperationResults.NOACK);
- break;
- case UNSENT:
- // Peer has not been sent to the peer: results in cancellation
- LOG.debug("Request {} was not sent when session went down, cancelling the instruction",
- e.getKey());
- r.done(OperationResults.UNSENT);
- break;
- default:
- break;
- }
- }
- requests.clear();
+ clearRequests();
}
}
}
*/
protected abstract boolean onMessage(MessageContext ctx, Message message);
+ // Non-final for mocking
@Override
public void close() {
synchronized (serverSessionManager) {
if (session != null) {
LOG.info("Closing session {}", session);
session.close(TerminationReason.UNKNOWN);
+ session = null;
}
+ syncOptimization = null;
+ clearRequests();
}
}
}
@Holding({"this.serverSessionManager", "this"})
private void clearNodeState() {
if (nodeState != null) {
- serverSessionManager.unbind(nodeState.getNodeId());
LOG.debug("Clear Node state: {}", nodeState.getNodeId());
+ if (listenerState != null) {
+ listenerState.close();
+ listenerState = null;
+ }
nodeState = null;
}
}
+ @Holding({"this.serverSessionManager", "this"})
+ private void clearRequests() {
+ // Clear all requests we know about
+ for (final Entry<SrpIdNumber, PCEPRequest> e : requests.entrySet()) {
+ // FIXME: exhaustive when we have JDK17+
+ switch (e.getValue().cancel()) {
+ case DONE:
+ // Done is done, nothing to do
+ LOG.trace("Request {} was done when session went down.", e.getKey());
+ break;
+ case UNACKED:
+ // Peer has not acked: results in failure
+ LOG.info("Request {} was incomplete when session went down, failing the instruction",
+ e.getKey());
+ break;
+ case UNSENT:
+ // Peer has not been sent to the peer: results in cancellation
+ LOG.debug("Request {} was not sent when session went down, cancelling the instruction",
+ e.getKey());
+ break;
+ default:
+ break;
+ }
+ }
+ requests.clear();
+ }
+
final synchronized PCEPRequest removeRequest(final SrpIdNumber id) {
final PCEPRequest ret = requests.remove(id);
if (ret != null && listenerState != null) {
- listenerState.processRequestStats(ret.getElapsedMillis());
+ // FIXME: just update fields
+ listenerState.getInstance().processRequestStats(ret.getElapsedMillis());
}
LOG.trace("Removed request {} object {}", id, ret);
return ret;
final synchronized ListenableFuture<OperationResult> sendMessage(final Message message, final SrpIdNumber requestId,
final Metadata metadata) {
final var sendFuture = session.sendMessage(message);
- listenerState.updateStatefulSentMsg(message);
- final PCEPRequest req = new PCEPRequest(metadata);
- requests.put(requestId, req);
- final short rpcTimeout = serverSessionManager.getRpcTimeout();
- LOG.trace("RPC response timeout value is {} seconds", rpcTimeout);
- if (rpcTimeout > 0) {
- setupTimeoutHandler(requestId, req, rpcTimeout);
+ // FIXME: just update fields
+ listenerState().updateStatefulSentMsg(message);
+
+ // Note: the timeout is held back by us holding the 'this' monitor, which timeoutExpired re-acquires
+ final var timeout = serverSessionManager.newRpcTimeout(this::timeoutExpired, requestId);
+ if (timeout != null) {
+ LOG.trace("Set up response timeout handler for request {}", requestId);
}
- sendFuture.addListener((FutureListener<Void>) future -> {
- if (!future.isSuccess()) {
- synchronized (AbstractTopologySessionListener.this) {
- requests.remove(requestId);
- }
- req.done(OperationResults.UNSENT);
- LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
- } else {
- req.sent();
- LOG.trace("Request {} sent to peer (object {})", requestId, req);
- }
- });
+ final PCEPRequest req = new PCEPRequest(metadata, timeout);
+ requests.put(requestId, req);
+ sendFuture.addListener(future -> sendCompleted(future, requestId, req));
return req.getFuture();
}
- private void setupTimeoutHandler(final SrpIdNumber requestId, final PCEPRequest req, final short timeout) {
- final Timer timer = req.getTimer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- synchronized (AbstractTopologySessionListener.this) {
- requests.remove(requestId);
- }
- req.done();
- LOG.info("Request {} timed-out waiting for response", requestId);
+ private void sendCompleted(final Future<?> future, final SrpIdNumber requestId, final PCEPRequest req) {
+ if (!future.isSuccess()) {
+ // FIXME: use concurrent operations and re-validate request vs. id
+ synchronized (AbstractTopologySessionListener.this) {
+ requests.remove(requestId);
}
- }, TimeUnit.SECONDS.toMillis(timeout));
- LOG.trace("Set up response timeout handler for request {}", requestId);
+ req.cancel();
+ LOG.info("Failed to send request {}, instruction cancelled", requestId, future.cause());
+ } else {
+ req.markUnacked();
+ LOG.trace("Request {} sent to peer (object {})", requestId, req);
+ }
+ }
+
+ private void timeoutExpired(final SrpIdNumber requestId) {
+ final PCEPRequest req;
+ synchronized (this) {
+ req = requests.remove(requestId);
+ }
+
+ if (req != null) {
+ LOG.info("Request {} timed-out waiting for response", requestId);
+ req.cancel();
+ }
}
/**
boolean incrementalSynchro);
final boolean isLspDbPersisted() {
- return syncOptimization != null && syncOptimization.isSyncAvoidanceEnabled();
+ return syncOptimization != null && syncOptimization.syncAvoidanceEnabled();
}
/**
* LSP-DB-VERSION TLV values doesnt match, and LSP-SYNC-CAPABILITY is enabled.
*/
final synchronized boolean isIncrementalSynchro() {
- return syncOptimization != null && syncOptimization.isSyncAvoidanceEnabled()
- && syncOptimization.isDeltaSyncEnabled();
+ return syncOptimization != null && syncOptimization.syncAvoidanceEnabled()
+ && syncOptimization.deltaSyncEnabled();
}
final synchronized boolean isTriggeredInitialSynchro() {
- return syncOptimization != null && syncOptimization.isTriggeredInitSyncEnabled();
+ return syncOptimization != null && syncOptimization.triggeredInitialSyncEnabled();
}
final synchronized boolean isTriggeredReSyncEnabled() {
- return syncOptimization != null && syncOptimization.isTriggeredReSyncEnabled();
+ return syncOptimization != null && syncOptimization.triggeredReSyncEnabled();
}
protected final synchronized boolean isSynchronized() {
- return syncOptimization != null && syncOptimization.doesLspDbMatch();
+ return syncOptimization != null && syncOptimization.dbVersionMatch();
}
@Override
.count());
}
+ @Override
+ public final long updateInterval() {
+ return serverSessionManager.updateInterval();
+ }
+
@Override
public final boolean isSessionSynchronized() {
return synced.get();
return lspUpdateCapability.get();
}
-
@Override
public synchronized ListenableFuture<RpcResult<Void>> tearDownSession(final TearDownSessionInput input) {
close();
- return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
+ return RpcResultBuilder.<Void>success().buildFuture();
+ }
+
+ final synchronized @NonNull SessionStateUpdater listenerState() {
+ return verifyNotNull(listenerState).getInstance();
}
static final class MessageContext {
private void notifyRequests() {
for (final PCEPRequest r : requests) {
- r.done(OperationResults.SUCCESS);
+ r.finish(OperationResults.SUCCESS);
}
}
}