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.util.ArrayList;
15 import java.util.HashMap;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeMXBean;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistration;
20 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.protocol.framework.SessionListenerFactory;
27 import org.opendaylight.protocol.pcep.PCEPSession;
28 import org.opendaylight.protocol.pcep.PCEPSessionListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
50 final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable, TopologySessionRPCs, PCEPTopologyProviderRuntimeMXBean {
51 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
52 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
54 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
55 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
56 private final TopologySessionListenerFactory listenerFactory;
57 private final InstanceIdentifier<Topology> topology;
58 private final DataBroker broker;
59 private Optional<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = Optional.absent();
61 public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
62 final TopologySessionListenerFactory listenerFactory) throws ReadFailedException, TransactionCommitFailedException {
63 this.broker = Preconditions.checkNotNull(broker);
64 this.topology = Preconditions.checkNotNull(topology);
65 this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
67 // Now create the base topology
68 final TopologyKey k = InstanceIdentifier.keyOf(topology);
69 final WriteTransaction tx = broker.newWriteOnlyTransaction();
70 tx.put(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
71 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
72 new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
73 new ArrayList<Node>()).build(), true);
74 tx.submit().checkedGet();
77 private static NodeId createNodeId(final InetAddress addr) {
78 return new NodeId("pcc://" + addr.getHostAddress());
81 synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session) {
82 LOG.debug("Node {} unbound", nodeState.getNodeId());
83 this.nodes.remove(createNodeId(session.getRemoteAddress()));
87 synchronized TopologyNodeState takeNodeState(final InetAddress address, final TopologySessionListener sessionListener) {
88 final NodeId id = createNodeId(address);
90 LOG.debug("Node {} requested by listener {}", id, sessionListener);
91 TopologyNodeState ret = this.state.get(id);
94 ret = new TopologyNodeState(this.broker, this.topology, id, DEFAULT_HOLD_STATE_NANOS);
95 LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
96 this.state.put(id, ret);
98 // FIXME: else check for conflicting session
101 this.nodes.put(id, sessionListener);
102 LOG.debug("Node {} bound to listener {}", id, sessionListener);
107 public PCEPSessionListener getSessionListener() {
108 return this.listenerFactory.createTopologySessionListener(this);
111 private synchronized TopologySessionListener checkSessionPresence(final NodeId nodeId) {
112 // Get the listener corresponding to the node
113 final TopologySessionListener l = this.nodes.get(nodeId);
115 LOG.debug("Session for node {} not found", nodeId);
122 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
123 final TopologySessionListener l = checkSessionPresence(input.getNode());
124 return (l != null) ? l.addLsp(input) : OperationResults.UNSENT.future();
128 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
129 final TopologySessionListener l = checkSessionPresence(input.getNode());
130 return (l != null) ? l.removeLsp(input) : OperationResults.UNSENT.future();
134 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
135 final TopologySessionListener l = checkSessionPresence(input.getNode());
136 return (l != null) ? l.updateLsp(input) : OperationResults.UNSENT.future();
140 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
141 final TopologySessionListener l = checkSessionPresence(input.getNode());
142 return (l != null) ? l.ensureLspOperational(input) : OperationResults.UNSENT.future();
146 public void close() throws TransactionCommitFailedException {
147 if (this.runtimeRootRegistration.isPresent()) {
148 this.runtimeRootRegistration.get().close();
150 for (final TopologySessionListener sessionListener : this.nodes.values()) {
151 sessionListener.close();
153 for (final TopologyNodeState nodeState : this.state.values()) {
156 final WriteTransaction t = this.broker.newWriteOnlyTransaction();
157 t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
158 t.submit().checkedGet();
161 public void registerRuntimeRootRegistartion(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
162 this.runtimeRootRegistration = Optional.of(runtimeRootRegistrator.register(this));
165 public Optional<PCEPTopologyProviderRuntimeRegistration> getRuntimeRootRegistration() {
166 return this.runtimeRootRegistration;