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.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;
25 public final class SalNodeWriter implements NodeWriter {
27 private static final Logger LOG = LoggerFactory.getLogger(SalNodeWriter.class);
29 private final DataBroker dataBroker;
30 private final String topologyId;
32 public SalNodeWriter(final DataBroker dataBroker, final String topologyId) {
33 this.dataBroker = dataBroker;
34 this.topologyId = topologyId;
37 //FIXME change to txChains
38 @Override public void init(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
40 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
41 wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
42 commitTransaction(wTx, id, "init");
45 @Override public void update(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
47 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
48 wTx.put(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId), operationalDataNode);
49 commitTransaction(wTx, id, "update");
52 @Override public void delete(@Nonnull final NodeId id) {
54 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
55 wTx.delete(LogicalDatastoreType.OPERATIONAL, TopologyUtil.createTopologyNodeListPath(new NodeKey(id), topologyId));
56 commitTransaction(wTx, id, "delete");
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();
64 Futures.addCallback(result, new FutureCallback<Void>() {
66 public void onSuccess(final Void result) {
67 LOG.debug("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
68 transaction.getIdentifier());
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);