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.util;
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 javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class SalNodeWriter implements NodeWriter {
32 private static final Logger LOG = LoggerFactory.getLogger(SalNodeWriter.class);
34 private final String topologyId;
36 private final BindingTransactionChain transactionChain;
37 public SalNodeWriter(final DataBroker dataBroker, final String topologyId) {
38 this.topologyId = topologyId;
39 this.transactionChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(new TransactionChainListener() {
41 public void onTransactionChainFailed(TransactionChain<?, ?> transactionChain, AsyncTransaction<?, ?> transaction, Throwable cause) {
42 LOG.error("{}: TransactionChain({}) {} FAILED!", transactionChain,
43 transaction.getIdentifier(), cause);
44 throw new IllegalStateException("Abstract topology writer TransactionChain(" + transactionChain + ") not committed correctly", cause);
48 public void onTransactionChainSuccessful(TransactionChain<?, ?> transactionChain) {
49 LOG.trace("Abstract topology writer TransactionChain({}) SUCCESSFUL", transactionChain);
54 @Override public void init(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
56 final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
57 wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
58 commitTransaction(wTx, id, "init");
61 @Override public void update(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
63 final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
64 wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
65 commitTransaction(wTx, id, "update");
68 @Override public void delete(@Nonnull final NodeId id) {
70 final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
71 wTx.delete(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId));
72 commitTransaction(wTx, id, "delete");
75 private void commitTransaction(final WriteTransaction transaction, final NodeId id, final String txType) {
76 LOG.debug("{}: Committing Transaction {}:{}", id.getValue(), txType,
77 transaction.getIdentifier());
78 final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
80 Futures.addCallback(result, new FutureCallback<Void>() {
82 public void onSuccess(final Void result) {
83 LOG.debug("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
84 transaction.getIdentifier());
88 public void onFailure(final Throwable t) {
89 LOG.error("{}: Transaction({}) {} FAILED!", id.getValue(), txType,
90 transaction.getIdentifier(), t);
91 throw new IllegalStateException(id + " Transaction(" + txType + ") not committed correctly", t);