2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.sal.connect.netconf;
10 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_CANDIDATE_QNAME;
11 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_COMMIT_QNAME;
12 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_CONFIG_QNAME;
13 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_EDIT_CONFIG_QNAME;
14 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_ERROR_OPTION_QNAME;
15 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_OPERATION_QNAME;
16 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_RUNNING_QNAME;
17 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_TARGET_QNAME;
18 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.ROLLBACK_ON_ERROR_OPTION;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
24 import java.util.Map.Entry;
25 import java.util.concurrent.ExecutionException;
27 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.Node;
37 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
38 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
39 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
44 import com.google.common.base.Preconditions;
45 import com.google.common.collect.ImmutableList;
46 import com.google.common.collect.Lists;
48 class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
50 private final DataModification<InstanceIdentifier, CompositeNode> modification;
51 private final NetconfDevice device;
52 private final boolean candidateSupported;
53 private final boolean rollbackSupported;
55 public NetconfDeviceTwoPhaseCommitTransaction(NetconfDevice device,
56 DataModification<InstanceIdentifier, CompositeNode> modification,
57 boolean candidateSupported, boolean rollbackOnErrorSupported) {
58 this.device = Preconditions.checkNotNull(device);
59 this.modification = Preconditions.checkNotNull(modification);
60 this.candidateSupported = candidateSupported;
61 this.rollbackSupported = rollbackOnErrorSupported;
64 void prepare() throws InterruptedException, ExecutionException {
65 for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) {
68 for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
69 sendMerge(toUpdate.getKey(),toUpdate.getValue());
73 private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
74 sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
77 private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
78 sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
81 private void sendEditRpc(CompositeNode editStructure) throws InterruptedException, ExecutionException {
82 CompositeNodeBuilder<ImmutableCompositeNode> builder = configurationRpcBuilder();
83 builder.setQName(NETCONF_EDIT_CONFIG_QNAME);
84 builder.add(editStructure);
86 RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
87 Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
90 private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
91 CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
94 if(candidateSupported) {
95 targetNode = ImmutableCompositeNode.create(NETCONF_CANDIDATE_QNAME, ImmutableList.<Node<?>>of());
97 targetNode = ImmutableCompositeNode.create(NETCONF_RUNNING_QNAME, ImmutableList.<Node<?>>of());
100 Node<?> targetWrapperNode = ImmutableCompositeNode.create(NETCONF_TARGET_QNAME, ImmutableList.<Node<?>>of(targetNode));
102 if(rollbackSupported) {
103 LOG.debug("Rollback-on-error supported, setting {} to {}", NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
104 ret.addLeaf(NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
107 ret.add(targetWrapperNode);
111 private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional<String> operation,
112 Optional<CompositeNode> lastChildOverride) {
113 List<PathArgument> path = dataPath.getPath();
114 List<PathArgument> reversed = Lists.reverse(path);
115 CompositeNode previous = null;
116 boolean isLast = true;
117 for (PathArgument arg : reversed) {
118 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
119 builder.setQName(arg.getNodeType());
120 Map<QName, Object> predicates = Collections.emptyMap();
121 if (arg instanceof NodeIdentifierWithPredicates) {
122 predicates = ((NodeIdentifierWithPredicates) arg).getKeyValues();
124 for (Entry<QName, Object> entry : predicates.entrySet()) {
125 builder.addLeaf(entry.getKey(), entry.getValue());
129 if (operation.isPresent()) {
130 builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
132 if (lastChildOverride.isPresent()) {
133 List<Node<?>> children = lastChildOverride.get().getChildren();
134 for(Node<?> child : children) {
135 if(!predicates.containsKey(child.getKey())) {
142 builder.add(previous);
144 previous = builder.toInstance();
147 return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
151 public RpcResult<Void> finish() {
152 CompositeNodeBuilder<ImmutableCompositeNode> commitInput = ImmutableCompositeNode.builder();
153 commitInput.setQName(NETCONF_COMMIT_QNAME);
155 final RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get();
156 return new RpcResult<Void>() {
159 public boolean isSuccessful() {
160 return rpcResult.isSuccessful();
164 public Void getResult() {
169 public Collection<RpcError> getErrors() {
170 return rpcResult.getErrors();
173 } catch (final InterruptedException | ExecutionException e) {
174 LOG.warn("Failed to finish operation", e);
175 return new RpcResult<Void>() {
177 public boolean isSuccessful() {
182 public Void getResult() {
187 public Collection<RpcError> getErrors() {
188 // FIXME: wrap the exception
189 return Collections.emptySet();
196 public DataModification<InstanceIdentifier, CompositeNode> getModification() {
197 return this.modification;
201 public RpcResult<Void> rollback() throws IllegalStateException {
202 // TODO Auto-generated method stub