Code Clean Up
[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.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23
24 public final class PCEPTunnelTopologyProvider implements AutoCloseable {
25     private final ListenerRegistration<NodeChangedListener> reg;
26     private final TopologyReference ref;
27
28     private PCEPTunnelTopologyProvider(final InstanceIdentifier<Topology> dst, final ListenerRegistration<NodeChangedListener> reg) {
29         this.ref = new DefaultTopologyReference(dst);
30         this.reg = Preconditions.checkNotNull(reg);
31     }
32
33     public static PCEPTunnelTopologyProvider create(final DataBroker dataProvider,
34             final InstanceIdentifier<Topology> sourceTopology, final TopologyId targetTopology) {
35         final InstanceIdentifier<Topology> dst = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
36                 new TopologyKey(targetTopology)).build();
37         final NodeChangedListener ncl = new NodeChangedListener(dataProvider, sourceTopology.firstKeyOf(Topology.class).getTopologyId(), dst);
38
39         final InstanceIdentifier<Node> src = sourceTopology.child(Node.class);
40         final ListenerRegistration<NodeChangedListener> reg = dataProvider.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, src), ncl);
41
42         return new PCEPTunnelTopologyProvider(dst, reg);
43     }
44
45     @Override
46     public void close() {
47         this.reg.close();
48     }
49
50     public TopologyReference getTopologyReference() {
51         return this.ref;
52     }
53 }