9eceeb352e3a040dc4ff8454c317989d4dce3f56
[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.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;
30
31 /**
32  * This class tracks the configuration content of a particular topology instance and propagates updates towards its
33  * associated {@link PCEPTopologyProvider}.
34  */
35 final class PCEPTopologyInstance implements ClusteredDataTreeChangeListener<Topology> {
36     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyInstance.class);
37
38     private final @NonNull TopologyKey topology;
39
40     @GuardedBy("this")
41     private PCEPTopologyProvider provider;
42     @GuardedBy("this")
43     private Registration reg;
44
45     PCEPTopologyInstance(final TopologyKey topology, final PCEPTopologyProviderDependencies dependencies,
46             final InstructionScheduler scheduler) {
47         this.topology = requireNonNull(topology);
48
49         final var instanceIdentifier = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology);
50
51         provider = new PCEPTopologyProvider(instanceIdentifier, dependencies, scheduler);
52
53         reg = dependencies.getDataBroker().registerDataTreeChangeListener(
54             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifier), this);
55         LOG.info("Topology instance for {} initialized", topologyId());
56     }
57
58     synchronized ListenableFuture<?> terminate() {
59         verifyNotNull(reg, "Topology %s instance %s already terminating", topologyId(), this);
60         reg.close();
61         reg = null;
62
63         final var ret = provider.stop();
64         provider = null;
65         return ret;
66     }
67
68     @Override
69     public synchronized void onDataTreeChanged(final Collection<DataTreeModification<Topology>> changes) {
70         if (reg == null) {
71             // We have been shut down, do not process any more updates
72             return;
73         }
74
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));
80         } else {
81             LOG.info("Topology {} configuration disappeared, ignoring update in anticipation of shutdown",
82                 topologyId());
83         }
84     }
85
86     private String topologyId() {
87         return TopologyUtils.friendlyId(topology);
88     }
89 }