Merge "BUG 652 leafref CCE & BUG 720 colons problem"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceTwoPhaseCommitTransaction.java
index 6df34cdb9efcb8093ff9e6dab3e535a28aaad1be..8a74b17ac46449c806f745c48e0892a59ec9374b 100644 (file)
@@ -1,15 +1,33 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.sal.connect.netconf;
 
+import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_CANDIDATE_QNAME;
+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;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Set;
+import java.util.concurrent.ExecutionException;
 
-import org.eclipse.xtext.xbase.lib.IterableExtensions;
-import org.opendaylight.controller.md.sal.common.api.data.DataModification;
 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.DataModification;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
@@ -17,73 +35,80 @@ import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifie
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
+import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 
-import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.*;
-
-public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
-
-    private NetconfDevice device;
+class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
     private final DataModification<InstanceIdentifier, CompositeNode> modification;
-    private boolean candidateSupported = true;
+    private final NetconfDevice device;
+    private final boolean candidateSupported;
+    private final boolean rollbackSupported;
 
     public NetconfDeviceTwoPhaseCommitTransaction(NetconfDevice device,
-            DataModification<InstanceIdentifier, CompositeNode> modification) {
-        super();
-        this.device = device;
-        this.modification = modification;
+            DataModification<InstanceIdentifier, CompositeNode> modification,
+            boolean candidateSupported, boolean rollbackOnErrorSupported) {
+        this.device = Preconditions.checkNotNull(device);
+        this.modification = Preconditions.checkNotNull(modification);
+        this.candidateSupported = candidateSupported;
+        this.rollbackSupported = rollbackOnErrorSupported;
     }
 
-    public void prepare() {
+    void prepare() throws InterruptedException, ExecutionException {
         for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) {
-            sendRemove(toRemove);
+            sendDelete(toRemove);
         }
         for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
             sendMerge(toUpdate.getKey(),toUpdate.getValue());
         }
-
     }
 
-    private void sendMerge(InstanceIdentifier key, CompositeNode value) {
+    private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
         sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
     }
 
-    private void sendRemove(InstanceIdentifier toRemove) {
-        sendEditRpc(createEditStructure(toRemove, Optional.of("remove"), Optional.<CompositeNode> absent()));
+    private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
+        sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
     }
 
-    private void sendEditRpc(CompositeNode editStructure) {
+    private void sendEditRpc(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());
+
+        RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
         Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
-        
     }
 
     private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
         CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
-        
+
         Node<?> targetNode;
         if(candidateSupported) {
             targetNode = ImmutableCompositeNode.create(NETCONF_CANDIDATE_QNAME, ImmutableList.<Node<?>>of());
         } 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> action,
+    private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional<String> operation,
             Optional<CompositeNode> lastChildOverride) {
         List<PathArgument> path = dataPath.getPath();
         List<PathArgument> reversed = Lists.reverse(path);
@@ -99,10 +124,10 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
             for (Entry<QName, Object> entry : predicates.entrySet()) {
                 builder.addLeaf(entry.getKey(), entry.getValue());
             }
-            
+
             if (isLast) {
-                if (action.isPresent()) {
-                    builder.setAttribute(NETCONF_ACTION_QNAME, action.get());
+                if (operation.isPresent()) {
+                    builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
                 }
                 if (lastChildOverride.isPresent()) {
                     List<Node<?>> children = lastChildOverride.get().getChildren();
@@ -111,7 +136,7 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
                             builder.add(child);
                         }
                     }
-                    
+
                 }
             } else {
                 builder.add(previous);
@@ -123,11 +148,48 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac
     }
 
     @Override
-    public RpcResult<Void> finish() throws IllegalStateException {
+    public RpcResult<Void> finish() {
         CompositeNodeBuilder<ImmutableCompositeNode> commitInput = ImmutableCompositeNode.builder();
         commitInput.setQName(NETCONF_COMMIT_QNAME);
-        RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance());
-        return (RpcResult<Void>) rpcResult;
+        try {
+            final RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get();
+            return new RpcResult<Void>() {
+
+                @Override
+                public boolean isSuccessful() {
+                    return rpcResult.isSuccessful();
+                }
+
+                @Override
+                public Void getResult() {
+                    return null;
+                }
+
+                @Override
+                public Collection<RpcError> getErrors() {
+                    return rpcResult.getErrors();
+                }
+            };
+        } catch (final InterruptedException | ExecutionException e) {
+            LOG.warn("Failed to finish operation", e);
+            return new RpcResult<Void>() {
+                @Override
+                public boolean isSuccessful() {
+                    return false;
+                }
+
+                @Override
+                public Void getResult() {
+                    return null;
+                }
+
+                @Override
+                public Collection<RpcError> getErrors() {
+                    // FIXME: wrap the exception
+                    return Collections.emptySet();
+                }
+            };
+        }
     }
 
     @Override