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;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collection;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
19 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
26 import org.opendaylight.yangtools.concepts.Registration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This class tracks the configuration content of a particular topology instance and propagates updates towards its
33 * associated {@link PCEPTopologyProvider}.
35 final class PCEPTopologyInstance implements ClusteredDataTreeChangeListener<Topology> {
36 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyInstance.class);
38 private final @NonNull TopologyKey topology;
41 private PCEPTopologyProvider provider;
43 private Registration reg;
45 PCEPTopologyInstance(final TopologyKey topology, final PCEPTopologyProviderDependencies dependencies,
46 final InstructionScheduler scheduler) {
47 this.topology = requireNonNull(topology);
49 final var instanceIdentifier = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology);
51 provider = new PCEPTopologyProvider(instanceIdentifier, dependencies, scheduler);
53 reg = dependencies.getDataBroker().registerDataTreeChangeListener(
54 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifier), this);
55 LOG.info("Topology instance for {} initialized", topologyId());
58 synchronized ListenableFuture<?> terminate() {
59 verifyNotNull(reg, "Topology %s instance %s already terminating", topologyId(), this);
63 final var ret = provider.stop();
69 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<Topology>> changes) {
71 // We have been shut down, do not process any more updates
75 // We are only interested in the after-image
76 final var content = Iterables.getLast(changes).getRootNode().getDataAfter();
77 if (content != null) {
78 LOG.trace("Updating topology {} configuration to {}", topologyId(), content);
79 provider.updateConfiguration(PCEPTopologyConfiguration.of(content));
81 LOG.info("Topology {} configuration disappeared, ignoring update in anticipation of shutdown",
86 private String topologyId() {
87 return TopologyUtils.friendlyId(topology);