Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / abstract-topology / src / main / java / org / opendaylight / netconf / topology / util / SalNodeWriter.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.netconf.topology.util;
10
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;
29
30 public final class SalNodeWriter implements NodeWriter {
31
32     private static final Logger LOG = LoggerFactory.getLogger(SalNodeWriter.class);
33
34     private final String topologyId;
35
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() {
40             @Override
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);
45             }
46
47             @Override
48             public void onTransactionChainSuccessful(TransactionChain<?, ?> transactionChain) {
49                 LOG.trace("Abstract topology writer TransactionChain({}) SUCCESSFUL", transactionChain);
50             }
51         });
52     }
53
54     @Override public void init(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
55         // put into Datastore
56         final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
57         wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
58         commitTransaction(wTx, id, "init");
59     }
60
61     @Override public void update(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
62         // merge
63         final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
64         wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
65         commitTransaction(wTx, id, "update");
66     }
67
68     @Override public void delete(@Nonnull final NodeId id) {
69         // delete
70         final WriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
71         wTx.delete(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId));
72         commitTransaction(wTx, id, "delete");
73     }
74
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();
79
80         Futures.addCallback(result, new FutureCallback<Void>() {
81             @Override
82             public void onSuccess(final Void result) {
83                 LOG.debug("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
84                         transaction.getIdentifier());
85             }
86
87             @Override
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);
92             }
93         });
94     }
95 }