Merge "Bug 615: Removed xtend from Topology Manager"
[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_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;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.concurrent.ExecutionException;
24
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;
39
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;
44
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;
50
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;
57     }
58
59     void prepare() throws InterruptedException, ExecutionException {
60         for (InstanceIdentifier toRemove : modification.getRemovedConfigurationData()) {
61             sendDelete(toRemove);
62         }
63         for(Entry<InstanceIdentifier, CompositeNode> toUpdate : modification.getUpdatedConfigurationData().entrySet()) {
64             sendMerge(toUpdate.getKey(),toUpdate.getValue());
65         }
66     }
67
68     private void sendMerge(InstanceIdentifier key, CompositeNode value) throws InterruptedException, ExecutionException {
69         sendEditRpc(createEditStructure(key, Optional.<String>absent(), Optional.of(value)));
70     }
71
72     private void sendDelete(InstanceIdentifier toDelete) throws InterruptedException, ExecutionException {
73         sendEditRpc(createEditStructure(toDelete, Optional.of("delete"), Optional.<CompositeNode> absent()));
74     }
75
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);
80
81         RpcResult<CompositeNode> rpcResult = device.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, builder.toInstance()).get();
82         Preconditions.checkState(rpcResult.isSuccessful(),"Rpc Result was unsuccessful");
83     }
84
85     private CompositeNodeBuilder<ImmutableCompositeNode> configurationRpcBuilder() {
86         CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
87
88         Node<?> targetNode;
89         if(candidateSupported) {
90             targetNode = ImmutableCompositeNode.create(NETCONF_CANDIDATE_QNAME, ImmutableList.<Node<?>>of());
91         } else {
92             targetNode = ImmutableCompositeNode.create(NETCONF_RUNNING_QNAME, ImmutableList.<Node<?>>of());
93         }
94         Node<?> targetWrapperNode = ImmutableCompositeNode.create(NETCONF_TARGET_QNAME, ImmutableList.<Node<?>>of(targetNode));
95         ret.add(targetWrapperNode);
96         return ret;
97     }
98
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();
111             }
112             for (Entry<QName, Object> entry : predicates.entrySet()) {
113                 builder.addLeaf(entry.getKey(), entry.getValue());
114             }
115
116             if (isLast) {
117                 if (operation.isPresent()) {
118                     builder.setAttribute(NETCONF_OPERATION_QNAME, operation.get());
119                 }
120                 if (lastChildOverride.isPresent()) {
121                     List<Node<?>> children = lastChildOverride.get().getChildren();
122                     for(Node<?> child : children) {
123                         if(!predicates.containsKey(child.getKey())) {
124                             builder.add(child);
125                         }
126                     }
127
128                 }
129             } else {
130                 builder.add(previous);
131             }
132             previous = builder.toInstance();
133             isLast = false;
134         }
135         return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
136     }
137
138     @Override
139     public RpcResult<Void> finish() {
140         CompositeNodeBuilder<ImmutableCompositeNode> commitInput = ImmutableCompositeNode.builder();
141         commitInput.setQName(NETCONF_COMMIT_QNAME);
142         try {
143             final RpcResult<?> rpcResult = device.invokeRpc(NetconfMapping.NETCONF_COMMIT_QNAME, commitInput.toInstance()).get();
144             return new RpcResult<Void>() {
145
146                 @Override
147                 public boolean isSuccessful() {
148                     return rpcResult.isSuccessful();
149                 }
150
151                 @Override
152                 public Void getResult() {
153                     return null;
154                 }
155
156                 @Override
157                 public Collection<RpcError> getErrors() {
158                     return rpcResult.getErrors();
159                 }
160             };
161         } catch (final InterruptedException | ExecutionException e) {
162             LOG.warn("Failed to finish operation", e);
163             return new RpcResult<Void>() {
164                 @Override
165                 public boolean isSuccessful() {
166                     return false;
167                 }
168
169                 @Override
170                 public Void getResult() {
171                     return null;
172                 }
173
174                 @Override
175                 public Collection<RpcError> getErrors() {
176                     // FIXME: wrap the exception
177                     return Collections.emptySet();
178                 }
179             };
180         }
181     }
182
183     @Override
184     public DataModification<InstanceIdentifier, CompositeNode> getModification() {
185         return this.modification;
186     }
187
188     @Override
189     public RpcResult<Void> rollback() throws IllegalStateException {
190         // TODO Auto-generated method stub
191         return null;
192     }
193 }