Bump upstreams
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / PCEPTopologyInstance.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.pcep.topology.provider;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.checkerframework.checker.lock.qual.GuardedBy;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
17 import org.opendaylight.mdsal.binding.api.DataListener;
18 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * This class tracks the configuration content of a particular topology instance and propagates updates towards its
30  * associated {@link PCEPTopologyProvider}.
31  */
32 final class PCEPTopologyInstance implements DataListener<Topology> {
33     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyInstance.class);
34
35     private final @NonNull TopologyKey topology;
36
37     @GuardedBy("this")
38     private PCEPTopologyProvider provider;
39     @GuardedBy("this")
40     private Registration reg;
41
42     PCEPTopologyInstance(final TopologyKey topology, final PCEPTopologyProviderDependencies dependencies,
43             final InstructionScheduler scheduler) {
44         this.topology = requireNonNull(topology);
45
46         final var instanceIdentifier = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology);
47
48         provider = new PCEPTopologyProvider(instanceIdentifier, dependencies, scheduler);
49
50         reg = dependencies.getDataBroker().registerDataListener(
51             DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, instanceIdentifier), this);
52         LOG.info("Topology instance for {} initialized", topologyId());
53     }
54
55     synchronized ListenableFuture<?> terminate() {
56         verifyNotNull(reg, "Topology %s instance %s already terminating", topologyId(), this);
57         reg.close();
58         reg = null;
59
60         final var ret = provider.stop();
61         provider = null;
62         return ret;
63     }
64
65     @Override
66     public synchronized void dataChangedTo(final Topology data) {
67         if (reg == null) {
68             // We have been shut down, do not process any more updates
69             return;
70         }
71
72         if (data != null) {
73             LOG.trace("Updating topology {} configuration to {}", topologyId(), data);
74             provider.updateConfiguration(PCEPTopologyConfiguration.of(data));
75         } else {
76             LOG.info("Topology {} configuration disappeared, ignoring update in anticipation of shutdown",
77                 topologyId());
78         }
79     }
80
81     private String topologyId() {
82         return TopologyUtils.friendlyId(topology);
83     }
84 }