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_OPERATION_QNAME;
15 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_RUNNING_QNAME;
16 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_TARGET_QNAME;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.concurrent.ExecutionException;
25 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.RpcError;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.Node;
35 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
36 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Optional;
41 import com.google.common.base.Preconditions;
42 import com.google.common.collect.ImmutableList;
43 import com.google.common.collect.Lists;
45 class NetconfDeviceTwoPhaseCommitTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
46 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTwoPhaseCommitTransaction.class);
47 private final DataModification<InstanceIdentifier, CompositeNode> modification;
48 private final NetconfDevice device;
49 private final boolean candidateSupported;
51 public NetconfDeviceTwoPhaseCommitTransaction(NetconfDevice device,
52 DataModification<InstanceIdentifier, CompositeNode> modification,
53 boolean candidateSupported) {
54 this.device = Preconditions.checkNotNull(device);
55 this.modification = Preconditions.checkNotNull(modification);
56 this.candidateSupported = candidateSupported;
59 void prepare() throws InterruptedException, ExecutionException {
60 for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) {
63 for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
64 sendMerge(toUpdate.getKey(),toUpdate.getValue());
68 private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
69 sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
72 private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
73 sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
76 private void sendEditRpc(CompositeNode editStructure) throws InterruptedException, ExecutionException {
77 CompositeNodeBuilder<ImmutableCompositeNode> builder = configurationRpcBuilder();
78 builder.setQName(NETCONF_EDIT_CONFIG_QNAME);
79 builder.add(editStructure);
81 RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
82 Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
85 private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
86 CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
89 if(candidateSupported) {
90 targetNode = ImmutableCompositeNode.create(NETCONF_CANDIDATE_QNAME, ImmutableList.<Node<?>>of());
92 targetNode = ImmutableCompositeNode.create(NETCONF_RUNNING_QNAME, ImmutableList.<Node<?>>of());
94 Node<?> targetWrapperNode = ImmutableCompositeNode.create(NETCONF_TARGET_QNAME, ImmutableList.<Node<?>>of(targetNode));
95 ret.add(targetWrapperNode);
99 private CompositeNode createEditStructure(InstanceIdentifier dataPath, Optional<String> operation,
100 Optional<CompositeNode> lastChildOverride) {
101 List<PathArgument> path = dataPath.getPath();
102 List<PathArgument> reversed = Lists.reverse(path);
103 CompositeNode previous = null;
104 boolean isLast = true;
105 for (PathArgument arg : reversed) {
106 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
107 builder.setQName(arg.getNodeType());
108 Map<QName, Object> predicates = Collections.emptyMap();
109 if (arg instanceof NodeIdentifierWithPredicates) {
110 predicates = ((NodeIdentifierWithPredicates) arg).getKeyValues();
112 for (Entry<QName, Object> entry : predicates.entrySet()) {
113 builder.addLeaf(entry.getKey(), entry.getValue());
117 if (operation.isPresent()) {
118 builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
120 if (lastChildOverride.isPresent()) {
121 List<Node<?>> children = lastChildOverride.get().getChildren();
122 for(Node<?> child : children) {
123 if(!predicates.containsKey(child.getKey())) {
130 builder.add(previous);
132 previous = builder.toInstance();
135 return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
139 public RpcResult<Void> finish() {
140 CompositeNodeBuilder<ImmutableCompositeNode> commitInput = ImmutableCompositeNode.builder();
141 commitInput.setQName(NETCONF_COMMIT_QNAME);
143 final RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get();
144 return new RpcResult<Void>() {
147 public boolean isSuccessful() {
148 return rpcResult.isSuccessful();
152 public Void getResult() {
157 public Collection<RpcError> getErrors() {
158 return rpcResult.getErrors();
161 } catch (final InterruptedException | ExecutionException e) {
162 LOG.warn("Failed to finish operation", e);
163 return new RpcResult<Void>() {
165 public boolean isSuccessful() {
170 public Void getResult() {
175 public Collection<RpcError> getErrors() {
176 // FIXME: wrap the exception
177 return Collections.emptySet();
184 public DataModification<InstanceIdentifier, CompositeNode> getModification() {
185 return this.modification;
189 public RpcResult<Void> rollback() throws IllegalStateException {
190 // TODO Auto-generated method stub