More netconf-topology module unit tests
[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.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public final class SalNodeWriter implements NodeWriter {
26
27     private static final Logger LOG = LoggerFactory.getLogger(SalNodeWriter.class);
28
29     private final DataBroker dataBroker;
30     private final String topologyId;
31
32     public SalNodeWriter(final DataBroker dataBroker, final String topologyId) {
33         this.dataBroker = dataBroker;
34         this.topologyId = topologyId;
35     }
36
37     //FIXME change to txChains
38     @Override public void init(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
39         // put into Datastore
40         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
41         wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
42         commitTransaction(wTx, id, "init");
43     }
44
45     @Override public void update(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
46         // merge
47         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
48         wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
49         commitTransaction(wTx, id, "update");
50     }
51
52     @Override public void delete(@Nonnull final NodeId id) {
53         // delete
54         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
55         wTx.delete(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId));
56         commitTransaction(wTx, id, "delete");
57     }
58
59     private void commitTransaction(final WriteTransaction transaction, final NodeId id, final String txType) {
60         LOG.debug("{}: Committing Transaction {}:{}", id.getValue(), txType,
61                 transaction.getIdentifier());
62         final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
63
64         Futures.addCallback(result, new FutureCallback<Void>() {
65             @Override
66             public void onSuccess(final Void result) {
67                 LOG.debug("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
68                         transaction.getIdentifier());
69             }
70
71             @Override
72             public void onFailure(final Throwable t) {
73                 LOG.error("{}: Transaction({}) {} FAILED!", id.getValue(), txType,
74                         transaction.getIdentifier(), t);
75                 throw new IllegalStateException(id + "  Transaction(" + txType + ") not committed correctly", t);
76             }
77         });
78     }
79 }