2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.pcep.topology.provider;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.util.HashMap;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicBoolean;
24 import org.checkerframework.checker.lock.qual.GuardedBy;
25 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyConfiguration;
26 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyProviderDependencies;
27 import org.opendaylight.bgpcep.pcep.topology.spi.stats.TopologySessionStatsRegistry;
28 import org.opendaylight.mdsal.binding.api.WriteTransaction;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
32 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
33 import org.opendaylight.protocol.pcep.PCEPSession;
34 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.PcepSessionState;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.AddLspArgs;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.EnsureLspOperationalInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.OperationResult;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.RemoveLspArgs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TearDownSessionInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TopologyTypes1Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TriggerSyncArgs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.UpdateLspArgs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.topology.pcep.type.TopologyPcepBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.RpcError;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 // Non-final for testing
63 class ServerSessionManager implements PCEPSessionListenerFactory, TopologySessionRPCs, PCEPPeerProposal,
64 TopologySessionStatsRegistry {
65 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
66 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
69 final AtomicBoolean isClosed = new AtomicBoolean(false);
71 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
73 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
74 private final InstanceIdentifier<Topology> topology;
75 private final PCEPStatefulPeerProposal peerProposal;
76 private final short rpcTimeout;
77 private final PCEPTopologyProviderDependencies dependenciesProvider;
78 private final PCEPDispatcherDependencies pcepDispatcherDependencies;
81 final PCEPTopologyProviderDependencies dependenciesProvider,
82 final PCEPTopologyConfiguration configDependencies) {
83 this.dependenciesProvider = requireNonNull(dependenciesProvider);
84 topology = requireNonNull(configDependencies.getTopology());
85 peerProposal = new PCEPStatefulPeerProposal(dependenciesProvider.getDataBroker(), topology);
86 rpcTimeout = configDependencies.getRpcTimeout();
87 pcepDispatcherDependencies = new PCEPDispatcherDependenciesImpl(this, configDependencies);
90 private static NodeId createNodeId(final InetAddress addr) {
91 return new NodeId("pcc://" + addr.getHostAddress());
95 * Create Base Topology.
97 final synchronized void instantiateServiceInstance() {
98 final TopologyKey key = InstanceIdentifier.keyOf(topology);
99 final TopologyId topologyId = key.getTopologyId();
100 final WriteTransaction tx = dependenciesProvider.getDataBroker().newWriteOnlyTransaction();
101 tx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder()
103 .setTopologyId(topologyId).setTopologyTypes(new TopologyTypesBuilder()
104 .addAugmentation(new TopologyTypes1Builder()
105 .setTopologyPcep(new TopologyPcepBuilder().build())
111 LOG.info("PCEP Topology {} created successfully.", topologyId.getValue());
112 ServerSessionManager.this.isClosed.set(false);
113 } catch (final ExecutionException | InterruptedException throwable) {
114 LOG.error("Failed to create PCEP Topology {}.", topologyId.getValue(), throwable);
115 ServerSessionManager.this.isClosed.set(true);
119 final synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session,
120 final boolean persistNode) {
121 if (isClosed.get()) {
122 LOG.error("Session Manager has already been closed.");
125 final NodeId nodeId = createNodeId(session.getRemoteAddress());
126 nodes.remove(nodeId);
127 state.remove(nodeId);
128 if (nodeState != null) {
129 LOG.debug("Node {} unbound", nodeState.getNodeId());
130 nodeState.released(persistNode);
134 final synchronized TopologyNodeState takeNodeState(final InetAddress address,
135 final TopologySessionListener sessionListener, final boolean retrieveNode) {
136 final NodeId id = createNodeId(address);
137 if (isClosed.get()) {
138 LOG.error("Server Session Manager is closed. Unable to create topology node {} with listener {}", id,
143 LOG.debug("Node {} requested by listener {}", id, sessionListener);
144 TopologyNodeState ret = state.get(id);
147 ret = new TopologyNodeState(dependenciesProvider.getDataBroker(), topology, id, DEFAULT_HOLD_STATE_NANOS);
148 LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
151 // if another listener requests the same session, close it
152 final TopologySessionListener existingSessionListener = nodes.get(id);
153 if (existingSessionListener != null && !sessionListener.equals(existingSessionListener)) {
154 LOG.error("New session listener {} is in conflict with existing session listener {} on node {},"
155 + " closing the existing one.", existingSessionListener, sessionListener, id);
156 existingSessionListener.close();
158 ret.taken(retrieveNode);
159 nodes.put(id, sessionListener);
160 LOG.debug("Node {} bound to listener {}", id, sessionListener);
164 // Non-final for testing
166 public PCEPTopologySessionListener getSessionListener() {
167 return new PCEPTopologySessionListener(this);
170 private synchronized TopologySessionListener checkSessionPresence(final NodeId nodeId) {
171 // Get the listener corresponding to the node
172 final TopologySessionListener l = nodes.get(nodeId);
174 LOG.debug("Session for node {} not found", nodeId);
181 public final synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
182 final TopologySessionListener l = checkSessionPresence(input.getNode());
183 return l != null ? l.addLsp(input) : OperationResults.UNSENT.future();
187 public final synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
188 final TopologySessionListener l = checkSessionPresence(input.getNode());
189 return l != null ? l.removeLsp(input) : OperationResults.UNSENT.future();
193 public final synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
194 final TopologySessionListener l = checkSessionPresence(input.getNode());
195 return l != null ? l.updateLsp(input) : OperationResults.UNSENT.future();
199 public final synchronized ListenableFuture<OperationResult> ensureLspOperational(
200 final EnsureLspOperationalInput input) {
201 final TopologySessionListener l = checkSessionPresence(input.getNode());
202 return l != null ? l.ensureLspOperational(input) : OperationResults.UNSENT.future();
206 public final synchronized ListenableFuture<OperationResult> triggerSync(final TriggerSyncArgs input) {
207 final TopologySessionListener l = checkSessionPresence(input.getNode());
208 return l != null ? l.triggerSync(input) : OperationResults.UNSENT.future();
212 public final ListenableFuture<RpcResult<Void>> tearDownSession(final TearDownSessionInput input) {
213 final NodeId nodeId = input.getNode();
214 final TopologySessionListener listener = checkSessionPresence(nodeId);
215 if (listener != null) {
216 return listener.tearDownSession(input);
219 return RpcResultBuilder.<Void>failed()
220 .withError(RpcError.ErrorType.RPC, "Failed to find session " + nodeId)
224 final synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
225 if (isClosed.getAndSet(true)) {
226 LOG.error("Session Manager has already been closed.");
227 return CommitInfo.emptyFluentFuture();
229 for (final TopologySessionListener node : nodes.values()) {
233 for (final TopologyNodeState topologyNodeState : state.values()) {
234 topologyNodeState.close();
238 final WriteTransaction t = dependenciesProvider.getDataBroker().newWriteOnlyTransaction();
239 t.delete(LogicalDatastoreType.OPERATIONAL, topology);
240 final FluentFuture<? extends CommitInfo> future = t.commit();
241 future.addCallback(new FutureCallback<CommitInfo>() {
243 public void onSuccess(final CommitInfo result) {
244 LOG.debug("Topology {} removed", topology);
248 public void onFailure(final Throwable throwable) {
249 LOG.warn("Failed to remove Topology {}", topology, throwable);
251 }, MoreExecutors.directExecutor());
256 public final void setPeerSpecificProposal(final InetSocketAddress address, final TlvsBuilder openBuilder) {
257 requireNonNull(address);
258 peerProposal.setPeerProposal(createNodeId(address.getAddress()), openBuilder,
259 pcepDispatcherDependencies.getSpeakerIdMapping().speakerIdForAddress(address.getAddress()));
262 final short getRpcTimeout() {
267 public final synchronized void bind(final KeyedInstanceIdentifier<Node, NodeKey> nodeId,
268 final PcepSessionState sessionState) {
269 dependenciesProvider.getStateRegistry().bind(nodeId, sessionState);
273 public final synchronized void unbind(final KeyedInstanceIdentifier<Node, NodeKey> nodeId) {
274 dependenciesProvider.getStateRegistry().unbind(nodeId);
277 final PCEPDispatcherDependencies getPCEPDispatcherDependencies() {
278 return pcepDispatcherDependencies;
281 final PCEPTopologyProviderDependencies getPCEPTopologyProviderDependencies() {
282 return dependenciesProvider;