5d7987bd0d8016fcdac97db87db0407bfff7b15e
[bgpcep.git] / pcep / tunnel-provider / src / main / java / org / opendaylight / bgpcep / pcep / tunnel / provider / PCEPTunnelTopologyProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.tunnel.provider;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
12 import org.opendaylight.bgpcep.topology.TopologyReference;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public final class PCEPTunnelTopologyProvider implements AutoCloseable {
26     private final ListenerRegistration<DataChangeListener> reg;
27     private final TopologyReference ref;
28
29     private PCEPTunnelTopologyProvider(final InstanceIdentifier<Topology> dst, final ListenerRegistration<DataChangeListener> reg) {
30         this.ref = new DefaultTopologyReference(dst);
31         this.reg = Preconditions.checkNotNull(reg);
32     }
33
34     public static PCEPTunnelTopologyProvider create(final DataBroker dataProvider,
35             final InstanceIdentifier<Topology> sourceTopology, final TopologyId targetTopology) {
36         final InstanceIdentifier<Topology> dst = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
37                 new TopologyKey(targetTopology)).build();
38         final NodeChangedListener ncl = new NodeChangedListener(dataProvider, sourceTopology.firstKeyOf(Topology.class, TopologyKey.class).getTopologyId(), dst);
39
40         final InstanceIdentifier<Node> src = sourceTopology.child(Node.class);
41         final ListenerRegistration<DataChangeListener> reg = dataProvider.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, src, ncl, DataChangeScope.SUBTREE);
42
43         return new PCEPTunnelTopologyProvider(dst, reg);
44     }
45
46     @Override
47     public void close() {
48         reg.close();
49     }
50
51     public TopologyReference getTopologyReference() {
52         return ref;
53     }
54 }