2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.bgpcep.pcep.topology.provider.TopologyUtils.friendlyId;
14 import java.util.Collection;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import org.checkerframework.checker.lock.qual.GuardedBy;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.bgpcep.pcep.server.PceServerProvider;
20 import org.opendaylight.bgpcep.pcep.topology.spi.stats.TopologySessionStatsRegistry;
21 import org.opendaylight.bgpcep.programming.spi.InstructionSchedulerFactory;
22 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.binding.api.RpcProviderService;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
29 import org.opendaylight.protocol.pcep.PCEPDispatcher;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.TopologyTypes1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.topology.pcep.type.TopologyPcep;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
36 import org.opendaylight.yangtools.concepts.Registration;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Primary entrypoint into this component. Once an instance of this class is instantiated, it will subscribe to
43 * changes to the configuration datastore. There it filters only topologies which have {@link TopologyPcep} type and for
44 * each one of those instantiates a cluster-wide singleton to handle lifecycle of services attached to that topology.
46 public final class PCEPTopologyTracker
47 implements PCEPTopologyProviderDependencies, ClusteredDataTreeChangeListener<TopologyPcep>, AutoCloseable {
48 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyTracker.class);
50 // Services we are using
51 final @NonNull InstructionSchedulerFactory instructionSchedulerFactory;
52 final @NonNull ClusterSingletonServiceProvider singletonService;
53 private final @NonNull TopologySessionStatsRegistry stateRegistry;
54 private final @NonNull RpcProviderService rpcProviderRegistry;
55 private final @NonNull PceServerProvider pceServerProvider;
56 private final @NonNull PCEPDispatcher pcepDispatcher;
57 private final @NonNull DataBroker dataBroker;
59 // We are reusing our monitor as the universal lock. We have to account for three distinct threads competing for
61 // 1) the typical DTCL callback thread invoking onDataTreeChanged()
62 // 2) instance cleanup thread invoking finishDestroy()
63 // 3) framework shutdown thread invoking close()
65 // We need to track not only instances which are deemed alive by the class, but also all instances for which cleanup
66 // has not finished yet, so close() can properly wait for cleanup to finish.
68 // Since close() will terminate the DTCL subscription, the synchronization between 1) and 3) is rather trivial.
70 // The interaction between DTCL and cleanup is tricky. DTCL can report rapid create/destroy/create events and
71 // cleanup is asynchronous and when the dust settles we need to end up in the corrected overall state (created or
74 // In order to achieve that without risking deadlocks, instances are tracked using a concurrent map and each
75 // 'create' edge allocates a new PCEPTopologyInstance object.
76 private final ConcurrentMap<TopologyKey, PCEPTopologySingleton> instances = new ConcurrentHashMap<>();
78 private Registration reg;
80 public PCEPTopologyTracker(final DataBroker dataBroker, final ClusterSingletonServiceProvider singletonService,
81 final RpcProviderService rpcProviderRegistry, final PCEPDispatcher pcepDispatcher,
82 final InstructionSchedulerFactory instructionSchedulerFactory,
83 final TopologySessionStatsRegistry stateRegistry, final PceServerProvider pceServerProvider) {
84 this.dataBroker = requireNonNull(dataBroker);
85 this.singletonService = requireNonNull(singletonService);
86 this.rpcProviderRegistry = requireNonNull(rpcProviderRegistry);
87 this.pcepDispatcher = requireNonNull(pcepDispatcher);
88 this.instructionSchedulerFactory = requireNonNull(instructionSchedulerFactory);
89 this.stateRegistry = requireNonNull(stateRegistry);
90 this.pceServerProvider = requireNonNull(pceServerProvider);
92 reg = dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
93 InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class).child(TopologyTypes.class)
94 .augmentation(TopologyTypes1.class).child(TopologyPcep.class).build()), this);
95 LOG.info("PCEP Topology tracker initialized");
99 public PCEPDispatcher getPCEPDispatcher() {
100 return pcepDispatcher;
104 public RpcProviderService getRpcProviderRegistry() {
105 return rpcProviderRegistry;
109 public DataBroker getDataBroker() {
114 public TopologySessionStatsRegistry getStateRegistry() {
115 return stateRegistry;
119 public PceServerProvider getPceServerProvider() {
120 return pceServerProvider;
124 public synchronized void close() {
126 // Already closed, bail out
130 LOG.info("PCEP Topology tracker shutting down");
134 // First pass: destroy all tracked instances
135 instances.values().forEach(PCEPTopologySingleton::destroy);
136 // Second pass: wait for cleanup
137 instances.values().forEach(PCEPTopologySingleton::awaitCleanup);
138 LOG.info("PCEP Topology tracker shut down");
142 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<TopologyPcep>> changes) {
144 // Registration has been terminated, do not process any changes
148 for (var change : changes) {
149 final var root = change.getRootNode();
150 switch (root.getModificationType()) {
152 // We only care if the topology has been newly introduced, not when its details have changed
153 if (root.getDataBefore() == null) {
154 createInstance(extractTopologyKey(change));
158 destroyInstance(extractTopologyKey(change));
166 private void createInstance(final @NonNull TopologyKey topology) {
167 final var existing = instances.remove(topology);
168 final PCEPTopologySingleton instance;
169 if (existing == null) {
170 LOG.info("Creating topology instance for {}", friendlyId(topology));
171 instance = new PCEPTopologySingleton(this, topology);
173 LOG.info("Resurrecting topology instance for {}", friendlyId(topology));
174 instance = existing.resurrect();
176 instances.put(topology, instance);
179 private void destroyInstance(final @NonNull TopologyKey topology) {
180 final var existing = instances.get(topology);
181 if (existing != null) {
182 LOG.info("Destroying topology instance for {}", friendlyId(topology));
185 LOG.warn("Attempted to destroy non-existent topology instance for {}", friendlyId(topology));
189 void finishDestroy(final TopologyKey topology, final PCEPTopologySingleton instance) {
190 if (instances.remove(topology, instance)) {
191 LOG.info("Destroyed topology instance of {}", friendlyId(topology));
195 private static @NonNull TopologyKey extractTopologyKey(final DataTreeModification<?> change) {
196 final var path = change.getRootPath().getRootIdentifier();
197 return verifyNotNull(path.firstKeyOf(Topology.class), "No topology key in %s", path);