X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fconnect%2Fnetconf%2FNetconfDeviceTwoPhaseCommitTransaction.java;h=5f14c264edb4cac6ab3dc1b81ccd8f2b1744a598;hp=c16ee170def8fe7fc13c24a15456058e0a356b8c;hb=7f0272398ce3dab7ceddd998c7bb510df3b28838;hpb=2c9ffa0406763f72332d60e3a49c57640ffafdcf diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/NetconfDeviceTwoPhaseCommitTransaction.java b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/NetconfDeviceTwoPhaseCommitTransaction.java index c16ee170de..5f14c264ed 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/NetconfDeviceTwoPhaseCommitTransaction.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/NetconfDeviceTwoPhaseCommitTransaction.java @@ -7,22 +7,25 @@ */ package org.opendaylight.controller.sal.connect.netconf; -import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_ACTION_QNAME; 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_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 java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; 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; @@ -31,51 +34,52 @@ 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.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.ImmutableList; import com.google.common.collect.Lists; -public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction { - - private final NetconfDevice device; +class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction { + private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class); private final DataModification modification; - private final boolean candidateSupported = true; + private final NetconfDevice device; + private final boolean candidateSupported; public NetconfDeviceTwoPhaseCommitTransaction(NetconfDevice device, - DataModification modification) { - super(); - this.device = device; - this.modification = modification; + DataModification modification, + boolean candidateSupported) { + this.device = Preconditions.checkNotNull(device); + this.modification = Preconditions.checkNotNull(modification); + this.candidateSupported = candidateSupported; } - public void prepare() { + void prepare() throws InterruptedException, ExecutionException { for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) { - sendRemove(toRemove); + sendDelete(toRemove); } for(Entry 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.absent(), Optional.of(value))); } - private void sendRemove(InstanceIdentifier toRemove) { - sendEditRpc(createEditStructure(toRemove, Optional.of("remove"), Optional. absent())); + private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException { + sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional. absent())); } - private void sendEditRpc(CompositeNode editStructure) { + private void sendEditRpc(CompositeNode editStructure) throws InterruptedException, ExecutionException { CompositeNodeBuilder builder = configurationRpcBuilder(); builder.setQName(NETCONF_EDIT_CONFIG_QNAME); builder.add(editStructure); - RpcResult rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()); + RpcResult rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get(); Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful"); - } private CompositeNodeBuilder configurationRpcBuilder() { @@ -92,7 +96,7 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac return ret; } - private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional action, + private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional operation, Optional lastChildOverride) { List path = dataPath.getPath(); List reversed = Lists.reverse(path); @@ -110,8 +114,8 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac } 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> children = lastChildOverride.get().getChildren(); @@ -132,11 +136,48 @@ public class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransac } @Override - public RpcResult finish() throws IllegalStateException { + public RpcResult finish() { CompositeNodeBuilder commitInput = ImmutableCompositeNode.builder(); commitInput.setQName(NETCONF_COMMIT_QNAME); - RpcResult rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()); - return (RpcResult) rpcResult; + try { + final RpcResult rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get(); + return new RpcResult() { + + @Override + public boolean isSuccessful() { + return rpcResult.isSuccessful(); + } + + @Override + public Void getResult() { + return null; + } + + @Override + public Collection getErrors() { + return rpcResult.getErrors(); + } + }; + } catch (final InterruptedException | ExecutionException e) { + LOG.warn("Failed to finish operation", e); + return new RpcResult() { + @Override + public boolean isSuccessful() { + return false; + } + + @Override + public Void getResult() { + return null; + } + + @Override + public Collection getErrors() { + // FIXME: wrap the exception + return Collections.emptySet(); + } + }; + } } @Override