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.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 DataBroker dataBroker;
35 private final String topologyId;
37 private final InstanceIdentifier<Topology> topologyListPath;
39 public SalNodeWriter(final DataBroker dataBroker, final String topologyId) {
40 this.dataBroker = dataBroker;
41 this.topologyId = topologyId;
42 this.topologyListPath = createTopologyId(this.topologyId);
45 //FIXME change to txChains
46 @Override public void init(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
48 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
49 wTx.put(LogicalDatastoreType.OPERATIONAL, createBindingPathForTopology(id), operationalDataNode);
50 commitTransaction(wTx, id, "init");
53 @Override public void update(@Nonnull final NodeId id, @Nonnull final Node operationalDataNode) {
55 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
56 wTx.put(LogicalDatastoreType.OPERATIONAL, createBindingPathForTopology(id), operationalDataNode);
57 commitTransaction(wTx, id, "update");
60 @Override public void delete(@Nonnull final NodeId id) {
62 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
63 wTx.delete(LogicalDatastoreType.OPERATIONAL, createBindingPathForTopology(id));
64 commitTransaction(wTx, id, "delete");
67 private void commitTransaction(final WriteTransaction transaction, final NodeId id, final String txType) {
68 LOG.debug("{}: Committing Transaction {}:{}", id.getValue(), txType,
69 transaction.getIdentifier());
70 final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
72 Futures.addCallback(result, new FutureCallback<Void>() {
74 public void onSuccess(final Void result) {
75 LOG.debug("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
76 transaction.getIdentifier());
80 public void onFailure(final Throwable t) {
81 LOG.error("{}: Transaction({}) {} FAILED!", id.getValue(), txType,
82 transaction.getIdentifier(), t);
83 throw new IllegalStateException(id + " Transaction(" + txType + ") not committed correctly", t);
88 private InstanceIdentifier<Node> createBindingPathForTopology(final NodeId id) {
89 return topologyListPath.child(Node.class, new NodeKey(id));
92 private InstanceIdentifier<Topology> createTopologyId(final String topologyId) {
93 final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.create(NetworkTopology.class);
94 return networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));