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.ReadWriteTransaction;
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.framework.SessionListenerFactory;
28 import org.opendaylight.protocol.pcep.PCEPSession;
29 import org.opendaylight.protocol.pcep.PCEPSessionListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspArgs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspArgs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.TopologyTypes1Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspArgs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.topology.pcep.type.TopologyPcepBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
53 final class ServerSessionManager implements SessionListenerFactory<PCEPSessionListener>, AutoCloseable, TopologySessionRPCs, PCEPTopologyProviderRuntimeMXBean {
54 private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
55 private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
57 private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
58 private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
59 private final TopologySessionListenerFactory listenerFactory;
60 private final InstanceIdentifier<Topology> topology;
61 private final DataBroker broker;
62 private Optional<PCEPTopologyProviderRuntimeRegistration> runtimeRootRegistration = Optional.absent();
64 public ServerSessionManager(final DataBroker broker, final InstanceIdentifier<Topology> topology,
65 final TopologySessionListenerFactory listenerFactory) throws ReadFailedException, TransactionCommitFailedException {
66 this.broker = Preconditions.checkNotNull(broker);
67 this.topology = Preconditions.checkNotNull(topology);
68 this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
71 // Make sure the topology does not exist
72 final ReadWriteTransaction tx = broker.newReadWriteTransaction();
73 final Optional<?> c = tx.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
74 Preconditions.checkArgument(!c.isPresent(), "Topology %s already exists", topology);
76 // create empty network-topology if not exists
77 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).build(), new NetworkTopologyBuilder().build());
78 // Now create the base topology
79 final TopologyKey k = InstanceIdentifier.keyOf(topology);
80 tx.put(LogicalDatastoreType.OPERATIONAL, topology, new TopologyBuilder().setKey(k).setTopologyId(k.getTopologyId()).setTopologyTypes(
81 new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
82 new TopologyTypes1Builder().setTopologyPcep(new TopologyPcepBuilder().build()).build()).build()).setNode(
83 new ArrayList<Node>()).build());
85 tx.submit().checkedGet();
88 private static NodeId createNodeId(final InetAddress addr) {
89 return new NodeId("pcc://" + addr.getHostAddress());
92 synchronized void releaseNodeState(final TopologyNodeState nodeState, final PCEPSession session) {
93 LOG.debug("Node {} unbound", nodeState.getNodeId());
94 this.nodes.remove(createNodeId(session.getRemoteAddress()));
98 synchronized TopologyNodeState takeNodeState(final InetAddress address, final TopologySessionListener sessionListener) {
99 final NodeId id = createNodeId(address);
101 LOG.debug("Node {} requested by listener {}", id, sessionListener);
102 TopologyNodeState ret = this.state.get(id);
105 ret = new TopologyNodeState(this.broker, this.topology, id, DEFAULT_HOLD_STATE_NANOS);
106 LOG.debug("Created topology node {} for id {} at {}", ret, id, ret.getNodeId());
107 this.state.put(id, ret);
109 // FIXME: else check for conflicting session
112 this.nodes.put(id, sessionListener);
113 LOG.debug("Node {} bound to listener {}", id, sessionListener);
118 public PCEPSessionListener getSessionListener() {
119 return this.listenerFactory.createTopologySessionListener(this);
123 public synchronized ListenableFuture<OperationResult> addLsp(final AddLspArgs input) {
124 // Get the listener corresponding to the node
125 final TopologySessionListener l = this.nodes.get(input.getNode());
127 LOG.debug("Session for node {} not found", input.getNode());
128 return OperationResults.UNSENT.future();
131 return l.addLsp(input);
135 public synchronized ListenableFuture<OperationResult> removeLsp(final RemoveLspArgs input) {
136 // Get the listener corresponding to the node
137 final TopologySessionListener l = this.nodes.get(input.getNode());
139 LOG.debug("Session for node {} not found", input.getNode());
140 return OperationResults.UNSENT.future();
143 return l.removeLsp(input);
147 public synchronized ListenableFuture<OperationResult> updateLsp(final UpdateLspArgs input) {
148 // Get the listener corresponding to the node
149 final TopologySessionListener l = this.nodes.get(input.getNode());
151 LOG.debug("Session for node {} not found", input.getNode());
152 return OperationResults.UNSENT.future();
155 return l.updateLsp(input);
159 public synchronized ListenableFuture<OperationResult> ensureLspOperational(final EnsureLspOperationalInput input) {
160 // Get the listener corresponding to the node
161 final TopologySessionListener l = this.nodes.get(input.getNode());
163 LOG.debug("Session for node {} not found", input.getNode());
164 return OperationResults.UNSENT.future();
167 return l.ensureLspOperational(input);
171 public void close() throws TransactionCommitFailedException {
172 if (this.runtimeRootRegistration.isPresent()) {
173 this.runtimeRootRegistration.get().close();
175 for (final TopologySessionListener sessionListener : this.nodes.values()) {
176 sessionListener.close();
178 for (final TopologyNodeState nodeState : this.state.values()) {
181 final WriteTransaction t = this.broker.newWriteOnlyTransaction();
182 t.delete(LogicalDatastoreType.OPERATIONAL, this.topology);
183 t.submit().checkedGet();
186 public void registerRuntimeRootRegistartion(final PCEPTopologyProviderRuntimeRegistrator runtimeRootRegistrator) {
187 this.runtimeRootRegistration = Optional.of(runtimeRootRegistrator.register(this));
190 public Optional<PCEPTopologyProviderRuntimeRegistration> getRuntimeRootRegistration() {
191 return this.runtimeRootRegistration;