8a74b17ac46449c806f745c48e0892a59ec9374b
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceTwoPhaseCommitTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.connect.netconf;
9
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;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.concurrent.ExecutionException;
26
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;
42
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;
47
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;
54
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;
62     }
63
64     void prepare() throws InterruptedException, ExecutionException {
65         for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) {
66             sendDelete(toRemove);
67         }
68         for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
69             sendMerge(toUpdate.getKey(),toUpdate.getValue());
70         }
71     }
72
73     private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
74         sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
75     }
76
77     private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
78         sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
79     }
80
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);
85
86         RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
87         Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
88     }
89
90     private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
91         CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
92
93         Node<?> targetNode;
94         if(candidateSupported) {
95             targetNode = ImmutableCompositeNode.create(NETCONF_CANDIDATE_QNAME, ImmutableList.<Node<?>>of());
96         } else {
97             targetNode = ImmutableCompositeNode.create(NETCONF_RUNNING_QNAME, ImmutableList.<Node<?>>of());
98         }
99
100         Node<?> targetWrapperNode = ImmutableCompositeNode.create(NETCONF_TARGET_QNAME, ImmutableList.<Node<?>>of(targetNode));
101
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);
105         }
106
107         ret.add(targetWrapperNode);
108         return ret;
109     }
110
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();
123             }
124             for (Entry<QName, Object> entry : predicates.entrySet()) {
125                 builder.addLeaf(entry.getKey(), entry.getValue());
126             }
127
128             if (isLast) {
129                 if (operation.isPresent()) {
130                     builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
131                 }
132                 if (lastChildOverride.isPresent()) {
133                     List<Node<?>> children = lastChildOverride.get().getChildren();
134                     for(Node<?> child : children) {
135                         if(!predicates.containsKey(child.getKey())) {
136                             builder.add(child);
137                         }
138                     }
139
140                 }
141             } else {
142                 builder.add(previous);
143             }
144             previous = builder.toInstance();
145             isLast = false;
146         }
147         return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
148     }
149
150     @Override
151     public RpcResult<Void> finish() {
152         CompositeNodeBuilder<ImmutableCompositeNode> commitInput = ImmutableCompositeNode.builder();
153         commitInput.setQName(NETCONF_COMMIT_QNAME);
154         try {
155             final RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get();
156             return new RpcResult<Void>() {
157
158                 @Override
159                 public boolean isSuccessful() {
160                     return rpcResult.isSuccessful();
161                 }
162
163                 @Override
164                 public Void getResult() {
165                     return null;
166                 }
167
168                 @Override
169                 public Collection<RpcError> getErrors() {
170                     return rpcResult.getErrors();
171                 }
172             };
173         } catch (final InterruptedException | ExecutionException e) {
174             LOG.warn("Failed to finish operation", e);
175             return new RpcResult<Void>() {
176                 @Override
177                 public boolean isSuccessful() {
178                     return false;
179                 }
180
181                 @Override
182                 public Void getResult() {
183                     return null;
184                 }
185
186                 @Override
187                 public Collection<RpcError> getErrors() {
188                     // FIXME: wrap the exception
189                     return Collections.emptySet();
190                 }
191             };
192         }
193     }
194
195     @Override
196     public DataModification<InstanceIdentifier, CompositeNode> getModification() {
197         return this.modification;
198     }
199
200     @Override
201     public RpcResult<Void> rollback() throws IllegalStateException {
202         // TODO Auto-generated method stub
203         return null;
204     }
205 }