2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.netconf.topology.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import java.util.concurrent.locks.ReentrantLock;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.netconf.topology.util.NodeWriter;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class TopologyNodeWriter implements NodeWriter{
42 private static final Logger LOG = LoggerFactory.getLogger(TopologyNodeWriter.class);
44 private final String topologyId;
45 private final BindingTransactionChain txChain;
47 private final InstanceIdentifier<NetworkTopology> networkTopologyPath;
48 private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyListPath;
50 private final ReentrantLock lock = new ReentrantLock(true);
52 public TopologyNodeWriter(final String topologyId, final DataBroker dataBroker) {
53 this.topologyId = topologyId;
54 this.txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(new TransactionChainListener() {
56 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
57 LOG.error("{}: TransactionChain({}) {} FAILED!", chain,
58 transaction.getIdentifier(), cause);
59 throw new IllegalStateException("Clustered topology writer TransactionChain(" + chain + ") not committed correctly", cause);
63 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
64 LOG.trace("Clustered topology writer TransactionChain({}) SUCCESSFUL", chain);
68 this.networkTopologyPath = InstanceIdentifier.builder(NetworkTopology.class).build();
69 this.topologyListPath = networkTopologyPath.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
71 // write an empty topology container at the start
72 final WriteTransaction wTx = txChain.newWriteOnlyTransaction();
73 createNetworkTopologyIfNotPresent(wTx, LogicalDatastoreType.OPERATIONAL);
74 createNetworkTopologyIfNotPresent(wTx, LogicalDatastoreType.CONFIGURATION);
75 commitTransaction(wTx, "init topology container", new NodeId("topology-netconf"));
79 public void init(@Nonnull NodeId id, @Nonnull Node operationalDataNode) {
82 final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
84 createNetworkTopologyIfNotPresent(writeTx, LogicalDatastoreType.OPERATIONAL);
85 final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
87 LOG.trace("{}: Init device state transaction {} putting if absent operational data started. Putting data on path {}",
88 id.getValue(), writeTx.getIdentifier(), path);
89 writeTx.put(LogicalDatastoreType.OPERATIONAL, path, operationalDataNode);
90 LOG.trace("{}: Init device state transaction {} putting operational data ended.",
91 id.getValue(), writeTx.getIdentifier());
93 commitTransaction(writeTx, "init", id);
100 public void update(@Nonnull NodeId id, @Nonnull Node operationalDataNode) {
103 final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
105 final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
106 LOG.trace("{}: Update device state transaction {} merging operational data started. Putting data on path {}",
107 id, writeTx.getIdentifier(), operationalDataNode);
108 writeTx.put(LogicalDatastoreType.OPERATIONAL, path, operationalDataNode);
109 LOG.trace("{}: Update device state transaction {} merging operational data ended.",
110 id, writeTx.getIdentifier());
112 commitTransaction(writeTx, "update", id);
120 public void delete(@Nonnull NodeId id) {
123 final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
125 final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
128 "{}: Close device state transaction {} removing all data started. Path: {}",
129 id, writeTx.getIdentifier(), path);
130 writeTx.delete(LogicalDatastoreType.OPERATIONAL, path);
132 "{}: Close device state transaction {} removing all data ended.",
133 id, writeTx.getIdentifier());
135 commitTransaction(writeTx, "close", id);
141 private void commitTransaction(final WriteTransaction transaction, final String txType, final NodeId id) {
142 LOG.trace("{}: Committing Transaction {}:{}", id.getValue(), txType,
143 transaction.getIdentifier());
144 final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
146 Futures.addCallback(result, new FutureCallback<Void>() {
148 public void onSuccess(final Void result) {
149 LOG.trace("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
150 transaction.getIdentifier());
154 public void onFailure(final Throwable t) {
155 LOG.error("{}: Transaction({}) {} FAILED!", id.getValue(), txType,
156 transaction.getIdentifier(), t);
157 throw new IllegalStateException(id.getValue() + " Transaction(" + txType + ") not committed correctly", t);
162 private void createNetworkTopologyIfNotPresent(final WriteTransaction writeTx, final LogicalDatastoreType datastoreType) {
164 final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
165 LOG.trace("{}: Merging {} container to ensure its presence", topologyId,
166 NetworkTopology.QNAME, writeTx.getIdentifier());
167 writeTx.merge(datastoreType, networkTopologyPath, networkTopology);
169 final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
170 LOG.trace("{}: Merging {} container to ensure its presence", topologyId,
171 Topology.QNAME, writeTx.getIdentifier());
172 writeTx.merge(datastoreType, topologyListPath, topology);
175 private static InstanceIdentifier<Node> createBindingPathForTopology(final NodeKey key, final String topologyId) {
176 final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.builder(NetworkTopology.class).build();
177 final KeyedInstanceIdentifier<Topology, TopologyKey> topology = networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
179 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));