Bug 1073: Implemented Transaction chain on InMemoryDOMDataStore level.
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceTwoPhaseCommitTransaction.java
index 9ec3aa3bb00bff78762770203ae79f7b80a94f3f..34cd9aa47b7f82702a797ddc4bea96ed34f28b7d 100644 (file)
@@ -11,9 +11,11 @@ import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NET
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_COMMIT_QNAME;
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_CONFIG_QNAME;
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_EDIT_CONFIG_QNAME;
+import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_ERROR_OPTION_QNAME;
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_OPERATION_QNAME;
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_RUNNING_QNAME;
 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_TARGET_QNAME;
+import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.ROLLBACK_ON_ERROR_OPTION;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -42,17 +44,20 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
-public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
+class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
-    private final NetconfDevice device;
     private final DataModification<InstanceIdentifier, CompositeNode> modification;
-    private final boolean candidateSupported = true;
-
-    public NetconfDeviceTwoPhaseCommitTransaction(NetconfDevice device,
-            DataModification<InstanceIdentifier, CompositeNode> modification) {
-        super();
-        this.device = device;
-        this.modification = modification;
+    private final NetconfDevice device;
+    private final boolean candidateSupported;
+    private final boolean rollbackSupported;
+
+    public NetconfDeviceTwoPhaseCommitTransaction(final NetconfDevice device,
+            final DataModification<InstanceIdentifier, CompositeNode> modification,
+            final boolean candidateSupported, final boolean rollbackOnErrorSupported) {
+        this.device = Preconditions.checkNotNull(device);
+        this.modification = Preconditions.checkNotNull(modification);
+        this.candidateSupported = candidateSupported;
+        this.rollbackSupported = rollbackOnErrorSupported;
     }
 
     void prepare() throws InterruptedException, ExecutionException {
@@ -62,25 +67,23 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
         for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
             sendMerge(toUpdate.getKey(),toUpdate.getValue());
         }
-
     }
 
-    private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
+    private void sendMerge(final InstanceIdentifier key, final CompositeNode value) throws InterruptedException, ExecutionException {
         sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
     }
 
-    private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
+    private void sendDelete(final InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
         sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
     }
 
-    private void sendEditRpc(CompositeNode editStructure) throws InterruptedException, ExecutionException {
+    private void sendEditRpc(final CompositeNode editStructure) throws InterruptedException, ExecutionException {
         CompositeNodeBuilder<ImmutableCompositeNode> builder = configurationRpcBuilder();
         builder.setQName(NETCONF_EDIT_CONFIG_QNAME);
         builder.add(editStructure);
 
         RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
         Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
-
     }
 
     private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
@@ -92,13 +95,20 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
         } else {
             targetNode = ImmutableCompositeNode.create(NETCONF_RUNNING_QNAME, ImmutableList.<Node<?>>of());
         }
+
         Node<?> targetWrapperNode = ImmutableCompositeNode.create(NETCONF_TARGET_QNAME, ImmutableList.<Node<?>>of(targetNode));
+
+        if(rollbackSupported) {
+            LOG.debug("Rollback-on-error supported, setting {} to {}", NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
+            ret.addLeaf(NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
+        }
+
         ret.add(targetWrapperNode);
         return ret;
     }
 
-    private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional<String> operation,
-            Optional<CompositeNode> lastChildOverride) {
+    private CompositeNode createEditStructure(final InstanceIdentifier dataPath, final Optional<String> operation,
+            final Optional<CompositeNode> lastChildOverride) {
         List<PathArgument> path = dataPath.getPath();
         List<PathArgument> reversed = Lists.reverse(path);
         CompositeNode previous = null;
@@ -119,7 +129,7 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
                     builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
                 }
                 if (lastChildOverride.isPresent()) {
-                    List<Node<?>> children = lastChildOverride.get().getChildren();
+                    List<Node<?>> children = lastChildOverride.get().getValue();
                     for(Node<?> child : children) {
                         if(!predicates.containsKey(child.getKey())) {
                             builder.add(child);