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 com.google.common.annotations.VisibleForTesting;
11 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.util.ArrayList;
19 import java.util.HashMap;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicBoolean;
23 import java.util.concurrent.atomic.AtomicReference;
24 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeMXBean;
26 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistration;
27 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
33 import org.opendaylight.protocol.pcep.PCEPSession;
34 import org.opendaylight.protocol.pcep.PCEPSessionListener;
35 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TriggerSyncArgs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.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.TopologyTypesBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
59 final class ServerSessionManager implements PCEPSessionListenerFactory, TopologySessionRPCs,
60 PCEPTopologyProviderRuntimeMXBean, PCEPPeerProposal {
61 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
62 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
65 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
67 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
68 private final TopologySessionListenerFactory listenerFactory;
69 private final InstanceIdentifier<Topology> topology;
70 private final DataBroker broker;
71 private final PCEPStatefulPeerProposal peerProposal;
72 private final short rpcTimeout;
73 private final AtomicReference<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = new AtomicReference<>();
76 public final AtomicBoolean isClosed = new AtomicBoolean(false);
78 public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
79 final TopologySessionListenerFactory listenerFactory, final short rpcTimeout) {
80 this.broker = requireNonNull(broker);
81 this.topology = requireNonNull(topology);
82 this.listenerFactory = requireNonNull(listenerFactory);
83 this.peerProposal = PCEPStatefulPeerProposal.createStatefulPeerProposal(this.broker, this.topology);
84 this.rpcTimeout = rpcTimeout;
88 * Create Base Topology
90 * @throws TransactionCommitFailedException exception
92 synchronized ListenableFuture<Void> instantiateServiceInstance() {
93 final TopologyKey key = InstanceIdentifier.keyOf(this.topology);
94 final TopologyId topologyId = key.getTopologyId();
95 final WriteTransaction tx = this.broker.newWriteOnlyTransaction();
96 tx.put(LogicalDatastoreType.OPERATIONAL, this.topology, new TopologyBuilder().setKey(key)
97 .setTopologyId(topologyId).setTopologyTypes(new TopologyTypesBuilder()
98 .addAugmentation(TopologyTypes1.class, new TopologyTypes1Builder().setTopologyPcep(
99 new TopologyPcepBuilder().build()).build()).build())
100 .setNode(new ArrayList<>()).build(), true);
101 final ListenableFuture<Void> future = tx.submit();
102 Futures.addCallback(future, new FutureCallback<Void>() {
104 public void onSuccess(final Void result) {
105 LOG.debug("PCEP Topology {} created successfully.", topologyId.getValue());
106 ServerSessionManager.this.isClosed.set(false);
110 public void onFailure(final Throwable t) {
111 LOG.error("Failed to create PCEP Topology {}.", topologyId.getValue(), t);
112 ServerSessionManager.this.isClosed.set(true);
114 }, MoreExecutors.directExecutor());
118 private static NodeId createNodeId(final InetAddress addr) {
119 return new NodeId("pcc://" + addr.getHostAddress());
122 synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session, final boolean persistNode) {
123 if (this.isClosed.get()) {
124 LOG.error("Session Manager has already been closed.");
127 this.nodes.remove(createNodeId(session.getRemoteAddress()));
128 if (nodeState != null) {
129 LOG.debug("Node {} unbound", nodeState.getNodeId());
130 nodeState.released(persistNode);
134 synchronized TopologyNodeState takeNodeState(final InetAddress address, final TopologySessionListener sessionListener, final boolean retrieveNode) {
135 final NodeId id = createNodeId(address);
136 if (this.isClosed.get()) {
137 LOG.error("Server Session Manager is closed. Unable to create topology node {} with listener {}", id, sessionListener);
141 LOG.debug("Node {} requested by listener {}", id, sessionListener);
142 TopologyNodeState ret = this.state.get(id);
145 ret = new TopologyNodeState(this.broker, this.topology, id, DEFAULT_HOLD_STATE_NANOS);
146 LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
147 this.state.put(id, ret);
149 // if another listener requests the same session, close it
150 final TopologySessionListener existingSessionListener = this.nodes.get(id);
151 if (existingSessionListener != null && !sessionListener.equals(existingSessionListener)) {
152 LOG.error("New session listener {} is in conflict with existing session listener {} on node {}, closing the existing one.", existingSessionListener, sessionListener, id);
153 existingSessionListener.close();
155 ret.taken(retrieveNode);
156 this.nodes.put(id, sessionListener);
157 LOG.debug("Node {} bound to listener {}", id, sessionListener);
162 public PCEPSessionListener getSessionListener() {
163 return this.listenerFactory.createTopologySessionListener(this);
166 private synchronized TopologySessionListener checkSessionPresence(final NodeId nodeId) {
167 // Get the listener corresponding to the node
168 final TopologySessionListener l = this.nodes.get(nodeId);
170 LOG.debug("Session for node {} not found", nodeId);
177 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
178 final TopologySessionListener l = checkSessionPresence(input.getNode());
179 return l != null ? l.addLsp(input) : OperationResults.UNSENT.future();
183 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
184 final TopologySessionListener l = checkSessionPresence(input.getNode());
185 return l != null ? l.removeLsp(input) : OperationResults.UNSENT.future();
189 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
190 final TopologySessionListener l = checkSessionPresence(input.getNode());
191 return l != null ? l.updateLsp(input) : OperationResults.UNSENT.future();
195 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
196 final TopologySessionListener l = checkSessionPresence(input.getNode());
197 return l != null ? l.ensureLspOperational(input) : OperationResults.UNSENT.future();
201 public synchronized ListenableFuture<OperationResult> triggerSync(final TriggerSyncArgs input) {
202 final TopologySessionListener l = checkSessionPresence(input.getNode());
203 return l != null ? l.triggerSync(input) : OperationResults.UNSENT.future();
206 synchronized ListenableFuture<Void> closeServiceInstance() {
207 if (this.isClosed.getAndSet(true)) {
208 LOG.error("Session Manager has already been closed.");
209 Futures.immediateFuture(null);
211 final PCEPTopologyProviderRuntimeRegistration runtimeReg = this.runtimeRootRegistration.getAndSet(null);
212 if (runtimeReg != null) {
215 for (final TopologySessionListener sessionListener : this.nodes.values()) {
216 sessionListener.close();
219 for (final TopologyNodeState nodeState : this.state.values()) {
223 final WriteTransaction t = this.broker.newWriteOnlyTransaction();
224 t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
225 final ListenableFuture<Void> future = t.submit();
226 Futures.addCallback(future, new FutureCallback<Void>() {
228 public void onSuccess(final Void result) {
229 LOG.debug("Topology {} removed", ServerSessionManager.this.topology);
233 public void onFailure(final Throwable t) {
234 LOG.warn("Failed to remove Topology {}", ServerSessionManager.this.topology, t);
236 }, MoreExecutors.directExecutor());
240 synchronized void setRuntimeRootRegistrator(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
241 if (!this.runtimeRootRegistration.compareAndSet(null, runtimeRootRegistrator.register(this))) {
242 LOG.error("Runtime root registration has been set before.");
246 PCEPTopologyProviderRuntimeRegistration getRuntimeRootRegistration() {
247 return this.runtimeRootRegistration.get();
251 public void setPeerSpecificProposal(final InetSocketAddress address, final TlvsBuilder openBuilder) {
252 requireNonNull(address);
253 this.peerProposal.setPeerProposal(createNodeId(address.getAddress()), openBuilder);
256 short getRpcTimeout() {
257 return this.rpcTimeout;