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.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
15 import java.util.ArrayList;
16 import java.util.HashMap;
18 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeMXBean;
20 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistration;
21 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
28 import org.opendaylight.protocol.pcep.PCEPSession;
29 import org.opendaylight.protocol.pcep.PCEPSessionListener;
30 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TriggerSyncArgs;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
54 final class ServerSessionManager implements PCEPSessionListenerFactory, AutoCloseable, TopologySessionRPCs, PCEPTopologyProviderRuntimeMXBean, PCEPPeerProposal {
55 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
56 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
58 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
59 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
60 private final TopologySessionListenerFactory listenerFactory;
61 private final InstanceIdentifier<Topology> topology;
62 private final DataBroker broker;
63 private final PCEPStatefulPeerProposal peerProposal;
64 private Optional<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = Optional.absent();
65 private final int rpcTimeout;
67 public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
68 final TopologySessionListenerFactory listenerFactory, final int rpcTimeout) throws ReadFailedException, TransactionCommitFailedException {
69 this.broker = Preconditions.checkNotNull(broker);
70 this.topology = Preconditions.checkNotNull(topology);
71 this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
72 this.peerProposal = PCEPStatefulPeerProposal.createStatefulPeerProposal(this.broker, this.topology);
73 this.rpcTimeout = rpcTimeout;
75 // Now create the base topology
76 final TopologyKey k = InstanceIdentifier.keyOf(topology);
77 final WriteTransaction tx = broker.newWriteOnlyTransaction();
78 tx.put(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
79 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
80 new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
81 new ArrayList<Node>()).build(), true);
82 tx.submit().checkedGet();
85 private static NodeId createNodeId(final InetAddress addr) {
86 return new NodeId("pcc://" + addr.getHostAddress());
89 synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session, final boolean persistNode) {
90 LOG.debug("Node {} unbound", nodeState.getNodeId());
91 this.nodes.remove(createNodeId(session.getRemoteAddress()));
92 nodeState.released(persistNode);
95 synchronized TopologyNodeState takeNodeState(final InetAddress address, final TopologySessionListener sessionListener, final boolean retrieveNode) {
96 final NodeId id = createNodeId(address);
98 LOG.debug("Node {} requested by listener {}", id, sessionListener);
99 TopologyNodeState ret = this.state.get(id);
102 ret = new TopologyNodeState(this.broker, this.topology, id, DEFAULT_HOLD_STATE_NANOS);
103 LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
104 this.state.put(id, ret);
106 // FIXME: else check for conflicting session
108 ret.taken(retrieveNode);
109 this.nodes.put(id, sessionListener);
110 LOG.debug("Node {} bound to listener {}", id, sessionListener);
115 public PCEPSessionListener getSessionListener() {
116 return this.listenerFactory.createTopologySessionListener(this);
119 private synchronized TopologySessionListener checkSessionPresence(final NodeId nodeId) {
120 // Get the listener corresponding to the node
121 final TopologySessionListener l = this.nodes.get(nodeId);
123 LOG.debug("Session for node {} not found", nodeId);
130 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
131 final TopologySessionListener l = checkSessionPresence(input.getNode());
132 return (l != null) ? l.addLsp(input) : OperationResults.UNSENT.future();
136 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
137 final TopologySessionListener l = checkSessionPresence(input.getNode());
138 return (l != null) ? l.removeLsp(input) : OperationResults.UNSENT.future();
142 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
143 final TopologySessionListener l = checkSessionPresence(input.getNode());
144 return (l != null) ? l.updateLsp(input) : OperationResults.UNSENT.future();
148 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
149 final TopologySessionListener l = checkSessionPresence(input.getNode());
150 return (l != null) ? l.ensureLspOperational(input) : OperationResults.UNSENT.future();
154 public synchronized ListenableFuture<OperationResult> triggerSync(final TriggerSyncArgs input) {
155 final TopologySessionListener l = checkSessionPresence(input.getNode());
156 return (l != null) ? l.triggerSync(input) : OperationResults.UNSENT.future();
160 public void close() throws TransactionCommitFailedException {
161 if (this.runtimeRootRegistration.isPresent()) {
162 this.runtimeRootRegistration.get().close();
164 for (final TopologySessionListener sessionListener : this.nodes.values()) {
165 sessionListener.close();
167 for (final TopologyNodeState nodeState : this.state.values()) {
170 final WriteTransaction t = this.broker.newWriteOnlyTransaction();
171 t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
172 t.submit().checkedGet();
175 public void registerRuntimeRootRegistartion(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
176 this.runtimeRootRegistration = Optional.of(runtimeRootRegistrator.register(this));
179 public Optional<PCEPTopologyProviderRuntimeRegistration> getRuntimeRootRegistration() {
180 return this.runtimeRootRegistration;
184 public void setPeerSpecificProposal(final InetSocketAddress address, final TlvsBuilder openBuilder) {
185 Preconditions.checkNotNull(address);
186 peerProposal.setPeerProposal(createNodeId(address.getAddress()), openBuilder);
189 public int getRpcTimeout() {
190 return this.rpcTimeout;