import javax.annotation.concurrent.GuardedBy;
import org.opendaylight.bgpcep.pcep.topology.provider.session.stats.SessionStateImpl;
import org.opendaylight.bgpcep.pcep.topology.provider.session.stats.TopologySessionStats;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.protocol.pcep.PCEPCloseTermination;
pccBuilder.setReportedLsp(initialNodeState.getAugmentation(Node1.class)
.getPathComputationClient().getReportedLsp());
}
- writeNode(pccBuilder, state, topologyAugment);
+ state.storeNode(topologyAugment,
+ new Node1Builder().setPathComputationClient(pccBuilder.build()).build(), this.session);
this.listenerState.init(psession);
LOG.info("Session with {} attached to topology node {}", psession.getRemoteAddress(), state.getNodeId());
}
- private void writeNode(final PathComputationClientBuilder pccBuilder, final TopologyNodeState state,
- final InstanceIdentifier<Node1> topologyAugment) {
- final Node1 ta = new Node1Builder().setPathComputationClient(pccBuilder.build()).build();
-
- final ReadWriteTransaction trans = state.rwTransaction();
- trans.put(LogicalDatastoreType.OPERATIONAL, topologyAugment, ta);
- LOG.trace("Peer data {} set to {}", topologyAugment, ta);
-
- // All set, commit the modifications
- Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
- @Override
- public void onSuccess(final Void result) {
- LOG.trace("Internal state for session {} updated successfully",
- AbstractTopologySessionListener.this.session);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.error("Failed to update internal state for session {}, terminating it",
- AbstractTopologySessionListener.this.session, throwable);
- AbstractTopologySessionListener.this.session.close(TerminationReason.UNKNOWN);
- }
- }, MoreExecutors.directExecutor());
- }
-
synchronized void updatePccState(final PccSyncState pccSyncState) {
if (this.nodeState == null) {
LOG.info("Server Session Manager is closed.");
AbstractTopologySessionListener.this.session.close(TerminationReason.UNKNOWN);
return;
}
- final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
+ final MessageContext ctx = new MessageContext(this.nodeState.getChain().newWriteOnlyTransaction());
updatePccNode(ctx, new PathComputationClientBuilder().setStateSync(pccSyncState).build());
if (pccSyncState != PccSyncState.Synchronized) {
this.synced.set(false);
Futures.addCallback(ctx.trans.submit(), new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
- LOG.trace("Internal state for session {} updated successfully",
+ LOG.trace("Pcc Internal state for session {} updated successfully",
AbstractTopologySessionListener.this.session);
}
@Override
public void onFailure(final Throwable throwable) {
- LOG.error("Failed to update internal state for session {}",
+ LOG.error("Failed to update Pcc internal state for session {}",
AbstractTopologySessionListener.this.session, throwable);
AbstractTopologySessionListener.this.session.close(TerminationReason.UNKNOWN);
}
psession.close(TerminationReason.UNKNOWN);
return;
}
- final MessageContext ctx = new MessageContext(this.nodeState.beginTransaction());
+ final MessageContext ctx = new MessageContext(this.nodeState.getChain().newWriteOnlyTransaction());
if (onMessage(ctx, message)) {
LOG.warn("Unhandled message {} on session {}", message, psession);
new TopologyProgramming(this.scheduler, this.manager)));
this.network.registerPath(NetworkTopologyContext.class, this.configDependencies.getTopology());
- this.manager.instantiateServiceInstance().get();
+ this.manager.instantiateServiceInstance();
final ChannelFuture channelFuture = this.dependenciesProvider.getPCEPDispatcher()
.createServer(this.manager.getPCEPDispatcherDependencies());
channelFuture.get();
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.concurrent.GuardedBy;
/**
* Create Base Topology.
*/
- synchronized ListenableFuture<Void> instantiateServiceInstance() {
+ synchronized void instantiateServiceInstance() {
final TopologyKey key = InstanceIdentifier.keyOf(this.topology);
final TopologyId topologyId = key.getTopologyId();
final WriteTransaction tx = this.dependenciesProvider.getDataBroker().newWriteOnlyTransaction();
.addAugmentation(TopologyTypes1.class, new TopologyTypes1Builder().setTopologyPcep(
new TopologyPcepBuilder().build()).build()).build())
.setNode(new ArrayList<>()).build(), true);
- final ListenableFuture<Void> future = tx.submit();
- Futures.addCallback(future, new FutureCallback<Void>() {
- @Override
- public void onSuccess(final Void result) {
- LOG.debug("PCEP Topology {} created successfully.", topologyId.getValue());
- ServerSessionManager.this.isClosed.set(false);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.error("Failed to create PCEP Topology {}.", topologyId.getValue(), throwable);
- ServerSessionManager.this.isClosed.set(true);
- }
- }, MoreExecutors.directExecutor());
- return future;
+ try {
+ tx.submit().get();
+ LOG.info("PCEP Topology {} created successfully.", topologyId.getValue());
+ ServerSessionManager.this.isClosed.set(false);
+ } catch (final ExecutionException | InterruptedException throwable) {
+ LOG.error("Failed to create PCEP Topology {}.", topologyId.getValue(), throwable);
+ ServerSessionManager.this.isClosed.set(true);
+ }
}
synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session,
}
@Override
- protected boolean onMessage(final MessageContext ctx, final Message message) {
+ protected synchronized boolean onMessage(final MessageContext ctx, final Message message) {
if (message instanceof PcerrMessage) {
return handleErrorMessage((PcerrMessage) message);
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
+import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.TerminationReason;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.Node1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.lsp.metadata.Metadata;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
private final long holdStateNanos;
private long lastReleased = 0;
//cache initial node state, if any node was persisted
+ @GuardedBy("this")
private Node initialNodeState = null;
TopologyNodeState(final DataBroker broker, final InstanceIdentifier<Topology> topology, final NodeId id,
// The session went down. Undo all the Topology changes we have done.
// We might want to persist topology node for later re-use.
if (!persist) {
- final WriteTransaction trans = beginTransaction();
+ final WriteTransaction trans = this.chain.newWriteOnlyTransaction();
trans.delete(LogicalDatastoreType.OPERATIONAL, this.nodeId);
Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
@Override
//try to get the topology's node
if (retrieveNode) {
- Futures.addCallback(readOperationalData(this.nodeId), new FutureCallback<Optional<Node>>() {
- @Override
- public void onSuccess(@Nonnull final Optional<Node> result) {
- if (!result.isPresent()) {
- putTopologyNode();
- } else {
- //cache retrieved node
- TopologyNodeState.this.initialNodeState = result.get();
- }
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.error("Failed to get topology node {}", TopologyNodeState.this.nodeId, throwable);
+ try {
+ final Optional<Node> prevNode = readOperationalData(this.nodeId).get();
+ if (!prevNode.isPresent()) {
+ putTopologyNode();
+ } else {
+ //cache retrieved node
+ TopologyNodeState.this.initialNodeState = prevNode.get();
}
- }, MoreExecutors.directExecutor());
+ } catch (final ExecutionException | InterruptedException throwable) {
+ LOG.error("Failed to get topology node {}", TopologyNodeState.this.nodeId, throwable);
+ }
} else {
putTopologyNode();
}
return this.initialNodeState;
}
- WriteTransaction beginTransaction() {
- return this.chain.newWriteOnlyTransaction();
- }
-
- ReadWriteTransaction rwTransaction() {
- return this.chain.newReadWriteTransaction();
+ synchronized BindingTransactionChain getChain() {
+ return this.chain;
}
- <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(final InstanceIdentifier<T> id) {
+ synchronized <T extends DataObject> ListenableFuture<Optional<T>> readOperationalData(
+ final InstanceIdentifier<T> id) {
try (ReadOnlyTransaction t = this.chain.newReadOnlyTransaction()) {
return t.read(LogicalDatastoreType.OPERATIONAL, id);
}
}
@Override
- public void close() {
+ public synchronized void close() {
this.chain.close();
}
- private void putTopologyNode() {
+ private synchronized void putTopologyNode() {
final Node node = new NodeBuilder().setKey(this.nodeId.getKey())
.setNodeId(this.nodeId.getKey().getNodeId()).build();
- final WriteTransaction t = beginTransaction();
- t.put(LogicalDatastoreType.OPERATIONAL, this.nodeId, node);
- t.submit();
+ final WriteTransaction t = this.chain.newWriteOnlyTransaction();
+ LOG.trace("Put topology Node {}, value {}", this.nodeId, node);
+ t.merge(LogicalDatastoreType.OPERATIONAL, this.nodeId, node);
+ Futures.addCallback(t.submit(), new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(final Void result) {
+ LOG.trace("Topology Node stored {}, value {}", TopologyNodeState.this.nodeId, node);
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.trace("Put topology Node failed {}, value {}, {}", TopologyNodeState.this.nodeId, node, throwable);
+ }
+ }, MoreExecutors.directExecutor());
}
+ public synchronized void storeNode(final InstanceIdentifier<Node1> topologyAugment, final Node1 ta,
+ final PCEPSession session) {
+ LOG.trace("Peer data {} set to {}", topologyAugment, ta);
+ final WriteTransaction trans = this.chain.newWriteOnlyTransaction();
+ trans.put(LogicalDatastoreType.OPERATIONAL, topologyAugment, ta);
+
+ // All set, commit the modifications
+ Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(final Void result) {
+ LOG.trace("Node stored {} for session {} updated successfully", topologyAugment, session);
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.error("Failed to store node {} for session {}, terminating it",
+ topologyAugment, session, throwable);
+ session.close(TerminationReason.UNKNOWN);
+ }
+ }, MoreExecutors.directExecutor());
+ }
}
\ No newline at end of file
@SuppressWarnings("checkstyle:IllegalCatch")
public synchronized void instantiateServiceInstance() {
LOG.info("PCEP Topology Provider Singleton Service {} instantiated", getIdentifier().getValue());
- if (this.pcepTopoProvider != null) {
- try {
- this.pcepTopoProvider.instantiateServiceInstance();
- } catch (final Exception e) {
- LOG.error("Failed to instantiate PCEP Topology provider", e);
- }
- this.serviceInstantiated = true;
+ try {
+ this.pcepTopoProvider.instantiateServiceInstance();
+ } catch (final Exception e) {
+ LOG.error("Failed to instantiate PCEP Topology provider", e);
}
+ this.serviceInstantiated = true;
}
@Override
public synchronized ListenableFuture<Void> closeServiceInstance() {
LOG.info("Close PCEP Topology Provider Singleton Service {}", getIdentifier().getValue());
- if (this.pcepTopoProvider != null && this.serviceInstantiated) {
+ if (this.serviceInstantiated) {
this.serviceInstantiated = false;
return this.pcepTopoProvider.closeServiceInstance();
}
import static org.mockito.Mockito.mock;
import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
-import com.google.common.util.concurrent.ListenableFuture;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
}
void startSessionManager() throws Exception {
- final ListenableFuture<Void> future = this.manager.instantiateServiceInstance();
- future.get();
+ this.manager.instantiateServiceInstance();
checkEquals(() -> assertFalse(this.manager.isClosed.get()));
}