Remove opendaylight directory
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / impl / TopologyNodeWriter.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.impl;
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 java.util.concurrent.locks.ReentrantLock;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.netconf.topology.util.NodeWriter;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class TopologyNodeWriter implements NodeWriter{
41
42     private static final Logger LOG = LoggerFactory.getLogger(TopologyNodeWriter.class);
43
44     private final String topologyId;
45     private final BindingTransactionChain txChain;
46
47     private final InstanceIdentifier<NetworkTopology> networkTopologyPath;
48     private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyListPath;
49
50     private final ReentrantLock lock = new ReentrantLock(true);
51
52     public TopologyNodeWriter(final String topologyId, final DataBroker dataBroker) {
53         this.topologyId = topologyId;
54         this.txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(new TransactionChainListener() {
55             @Override
56             public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
57                 LOG.error("{}: TransactionChain({}) {} FAILED!", chain,
58                         transaction.getIdentifier(), cause);
59                 throw new IllegalStateException("Clustered topology writer TransactionChain(" + chain + ") not committed correctly", cause);
60             }
61
62             @Override
63             public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
64                 LOG.trace("Clustered topology writer TransactionChain({}) SUCCESSFUL", chain);
65             }
66         });
67
68         this.networkTopologyPath = InstanceIdentifier.builder(NetworkTopology.class).build();
69         this.topologyListPath = networkTopologyPath.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
70
71         // write an empty topology container at the start
72         final WriteTransaction wTx = txChain.newWriteOnlyTransaction();
73         createNetworkTopologyIfNotPresent(wTx, LogicalDatastoreType.OPERATIONAL);
74         createNetworkTopologyIfNotPresent(wTx, LogicalDatastoreType.CONFIGURATION);
75         commitTransaction(wTx, "init topology container", new NodeId("topology-netconf"));
76     }
77
78     @Override
79     public void init(@Nonnull NodeId id, @Nonnull Node operationalDataNode) {
80         lock.lock();
81         try {
82             final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
83
84             createNetworkTopologyIfNotPresent(writeTx, LogicalDatastoreType.OPERATIONAL);
85             final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
86
87             LOG.trace("{}: Init device state transaction {} putting if absent operational data started. Putting data on path {}",
88                     id.getValue(), writeTx.getIdentifier(), path);
89             writeTx.put(LogicalDatastoreType.OPERATIONAL, path, operationalDataNode);
90             LOG.trace("{}: Init device state transaction {} putting operational data ended.",
91                     id.getValue(), writeTx.getIdentifier());
92
93             commitTransaction(writeTx, "init", id);
94         } finally {
95             lock.unlock();
96         }
97     }
98
99     @Override
100     public void update(@Nonnull NodeId id, @Nonnull Node operationalDataNode) {
101         lock.lock();
102         try {
103             final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
104
105             final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
106             LOG.trace("{}: Update device state transaction {} merging operational data started. Putting data on path {}",
107                     id, writeTx.getIdentifier(), operationalDataNode);
108             writeTx.put(LogicalDatastoreType.OPERATIONAL, path, operationalDataNode);
109             LOG.trace("{}: Update device state transaction {} merging operational data ended.",
110                     id, writeTx.getIdentifier());
111
112             commitTransaction(writeTx, "update", id);
113         } finally {
114             lock.unlock();
115         }
116
117     }
118
119     @Override
120     public void delete(@Nonnull NodeId id) {
121         lock.lock();
122         try {
123             final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
124
125             final InstanceIdentifier<Node> path = createBindingPathForTopology(new NodeKey(id), topologyId);
126
127             LOG.trace(
128                     "{}: Close device state transaction {} removing all data started. Path: {}",
129                     id, writeTx.getIdentifier(), path);
130             writeTx.delete(LogicalDatastoreType.OPERATIONAL, path);
131             LOG.trace(
132                     "{}: Close device state transaction {} removing all data ended.",
133                     id, writeTx.getIdentifier());
134
135             commitTransaction(writeTx, "close", id);
136         } finally {
137             lock.unlock();
138         }
139     }
140
141     private void commitTransaction(final WriteTransaction transaction, final String txType, final NodeId id) {
142         LOG.trace("{}: Committing Transaction {}:{}", id.getValue(), txType,
143                 transaction.getIdentifier());
144         final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
145
146         Futures.addCallback(result, new FutureCallback<Void>() {
147             @Override
148             public void onSuccess(final Void result) {
149                 LOG.trace("{}: Transaction({}) {} SUCCESSFUL", id.getValue(), txType,
150                         transaction.getIdentifier());
151             }
152
153             @Override
154             public void onFailure(final Throwable t) {
155                 LOG.error("{}: Transaction({}) {} FAILED!", id.getValue(), txType,
156                         transaction.getIdentifier(), t);
157                 throw new IllegalStateException(id.getValue() + "  Transaction(" + txType + ") not committed correctly", t);
158             }
159         });
160     }
161
162     private void createNetworkTopologyIfNotPresent(final WriteTransaction writeTx, final LogicalDatastoreType datastoreType) {
163
164         final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
165         LOG.trace("{}: Merging {} container to ensure its presence", topologyId,
166                 NetworkTopology.QNAME, writeTx.getIdentifier());
167         writeTx.merge(datastoreType, networkTopologyPath, networkTopology);
168
169         final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
170         LOG.trace("{}: Merging {} container to ensure its presence", topologyId,
171                 Topology.QNAME, writeTx.getIdentifier());
172         writeTx.merge(datastoreType, topologyListPath, topology);
173     }
174
175     private static InstanceIdentifier<Node> createBindingPathForTopology(final NodeKey key, final String topologyId) {
176         final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.builder(NetworkTopology.class).build();
177         final KeyedInstanceIdentifier<Topology, TopologyKey> topology = networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
178         return topology
179                 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));
180     }
181 }