Bump MRI upstreams
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfBaseOps.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.netconf.sal.connect.netconf.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.COMMIT_RPC_CONTENT;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.EDIT_CONTENT_NODEID;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_RPC_CONTENT;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_NODEID;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DEFAULT_OPERATION_NODEID;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_NODEID;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_ERROR_OPTION_NODEID;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_NODEID;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_NODEID;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
31 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_SOURCE_NODEID;
32 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_NODEID;
33 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_NODEID;
34 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
35 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_VALIDATE_NODEID;
36 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME;
37 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.ROLLBACK_ON_ERROR_OPTION;
38 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
39 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
40
41 import com.google.common.collect.Iterables;
42 import com.google.common.util.concurrent.FutureCallback;
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.ListenableFuture;
45 import com.google.common.util.concurrent.MoreExecutors;
46 import java.util.AbstractMap.SimpleEntry;
47 import java.util.Collections;
48 import java.util.List;
49 import java.util.Locale;
50 import java.util.Map;
51 import java.util.Optional;
52 import java.util.stream.Collectors;
53 import org.eclipse.jdt.annotation.NonNull;
54 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
55 import org.opendaylight.mdsal.dom.api.DOMRpcService;
56 import org.opendaylight.netconf.api.ModifyAction;
57 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade.KeepaliveDOMRpcService;
58 import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.config.input.source.ConfigSource;
61 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
62 import org.opendaylight.yangtools.yang.common.Empty;
63 import org.opendaylight.yangtools.yang.common.QName;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
69 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
71 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
72 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
73
74 /**
75  * Provides base operations for netconf e.g. get, get-config, edit-config, (un)lock, commit etc.
76  * According to RFC-6241
77  */
78 public final class NetconfBaseOps {
79     private static final NodeIdentifier CONFIG_SOURCE_NODEID = NodeIdentifier.create(ConfigSource.QNAME);
80     private static final NodeIdentifier CONFIG_TARGET_NODEID = NodeIdentifier.create(ConfigTarget.QNAME);
81
82     private final DOMRpcService rpc;
83     private final MountPointContext mountContext;
84     private final RpcStructureTransformer transformer;
85
86     public NetconfBaseOps(final DOMRpcService rpc, final MountPointContext mountContext) {
87         this.rpc = rpc;
88         this.mountContext = mountContext;
89
90         if (rpc instanceof KeepaliveDOMRpcService
91                 && ((KeepaliveDOMRpcService) rpc).getDeviceRpc() instanceof SchemalessNetconfDeviceRpc) {
92             this.transformer = new SchemalessRpcStructureTransformer();
93         } else {
94             this.transformer = new NetconfRpcStructureTransformer(mountContext);
95         }
96     }
97
98     public ListenableFuture<? extends DOMRpcResult> lock(final FutureCallback<DOMRpcResult> callback,
99             final QName datastore) {
100         requireNonNull(callback);
101         requireNonNull(datastore);
102
103         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
104                 getLockContent(datastore));
105         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
106         return future;
107     }
108
109     public ListenableFuture<? extends DOMRpcResult> lockCandidate(final FutureCallback<DOMRpcResult> callback) {
110         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
111             getLockContent(NETCONF_CANDIDATE_QNAME));
112         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
113         return future;
114     }
115
116     public ListenableFuture<? extends DOMRpcResult> lockRunning(final FutureCallback<DOMRpcResult> callback) {
117         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
118             getLockContent(NETCONF_RUNNING_QNAME));
119         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
120         return future;
121     }
122
123     public ListenableFuture<? extends DOMRpcResult> unlock(final FutureCallback<DOMRpcResult> callback,
124             final QName datastore) {
125         requireNonNull(callback);
126         requireNonNull(datastore);
127
128         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
129                 getUnLockContent(datastore));
130         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
131         return future;
132     }
133
134     public ListenableFuture<? extends DOMRpcResult> unlockRunning(final FutureCallback<DOMRpcResult> callback) {
135         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
136             getUnLockContent(NETCONF_RUNNING_QNAME));
137         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
138         return future;
139     }
140
141     public ListenableFuture<? extends DOMRpcResult> unlockCandidate(final FutureCallback<DOMRpcResult> callback) {
142         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
143             getUnLockContent(NETCONF_CANDIDATE_QNAME));
144         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
145         return future;
146     }
147
148     public ListenableFuture<? extends DOMRpcResult> discardChanges(final FutureCallback<DOMRpcResult> callback) {
149         requireNonNull(callback);
150
151         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_DISCARD_CHANGES_QNAME, null);
152         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
153         return future;
154     }
155
156     public ListenableFuture<? extends DOMRpcResult> commit(final FutureCallback<DOMRpcResult> callback) {
157         requireNonNull(callback);
158
159         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_COMMIT_QNAME, COMMIT_RPC_CONTENT);
160         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
161         return future;
162     }
163
164     public ListenableFuture<? extends DOMRpcResult> validate(final FutureCallback<DOMRpcResult> callback,
165             final QName datastore) {
166         requireNonNull(callback);
167
168         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_VALIDATE_QNAME,
169             getValidateContent(requireNonNull(datastore)));
170         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
171         return future;
172     }
173
174     public ListenableFuture<? extends DOMRpcResult> validateCandidate(final FutureCallback<DOMRpcResult> callback) {
175         return validate(callback, NETCONF_CANDIDATE_QNAME);
176     }
177
178     public ListenableFuture<? extends DOMRpcResult> validateRunning(final FutureCallback<DOMRpcResult> callback) {
179         return validate(callback, NETCONF_RUNNING_QNAME);
180     }
181
182     public ListenableFuture<? extends DOMRpcResult> copyConfig(final FutureCallback<DOMRpcResult> callback,
183                                                                final QName source, final QName target) {
184         requireNonNull(callback);
185
186         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_COPY_CONFIG_QNAME,
187             getCopyConfigContent(requireNonNull(source), requireNonNull(target)));
188         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
189         return future;
190     }
191
192     public ListenableFuture<? extends DOMRpcResult> copyRunningToCandidate(
193             final FutureCallback<DOMRpcResult> callback) {
194         return copyConfig(callback, NETCONF_RUNNING_QNAME, NETCONF_CANDIDATE_QNAME);
195     }
196
197     public ListenableFuture<? extends DOMRpcResult> getConfig(final FutureCallback<DOMRpcResult> callback,
198                                                               final QName datastore,
199                                                               final Optional<YangInstanceIdentifier> filterPath) {
200         requireNonNull(callback);
201         requireNonNull(datastore);
202
203         final ListenableFuture<? extends DOMRpcResult> future;
204         if (isFilterPresent(filterPath)) {
205             final DataContainerChild node = transformer.toFilterStructure(filterPath.get());
206             future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
207                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore), node));
208         } else {
209             future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
210                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore)));
211         }
212
213         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
214         return future;
215     }
216
217     private ListenableFuture<? extends DOMRpcResult> getConfig(final FutureCallback<DOMRpcResult> callback,
218             final QName datastore, final Optional<YangInstanceIdentifier> filterPath,
219             final List<YangInstanceIdentifier> fields) {
220         requireNonNull(callback);
221         requireNonNull(datastore);
222
223         final NormalizedNode rpcInput;
224         if (isFilterPresent(filterPath)) {
225             final DataContainerChild node = transformer.toFilterStructure(
226                     Collections.singletonList(FieldsFilter.of(filterPath.get(), fields)));
227             rpcInput = NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore), node);
228         } else if (containsEmptyPath(fields)) {
229             rpcInput = NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore));
230         } else {
231             final DataContainerChild subtreeFilter = getSubtreeFilterFromRootFields(fields);
232             rpcInput = NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
233                     getSourceNode(datastore), subtreeFilter);
234         }
235         final ListenableFuture<? extends DOMRpcResult> response = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME, rpcInput);
236         Futures.addCallback(response, callback, MoreExecutors.directExecutor());
237         return response;
238     }
239
240     /**
241      * Calling GET-CONFIG RPC with subtree filter that is specified by {@link YangInstanceIdentifier}.
242      *
243      * @param callback   RPC response callback
244      * @param filterPath path to requested data
245      * @return asynchronous completion token with read {@link NormalizedNode} wrapped in {@link Optional} instance
246      */
247     public ListenableFuture<Optional<NormalizedNode>> getConfigRunningData(final FutureCallback<DOMRpcResult> callback,
248             final Optional<YangInstanceIdentifier> filterPath) {
249         return extractData(filterPath, getConfigRunning(callback, filterPath));
250     }
251
252     /**
253      * Calling GET-CONFIG RPC with subtree filter tha tis specified by parent {@link YangInstanceIdentifier} and list
254      * of specific fields that caller would like to read. Field paths are relative to parent path.
255      *
256      * @param callback   RPC response callback
257      * @param filterPath parent path to requested data
258      * @param fields     paths to specific fields that are selected under parent path
259      * @return asynchronous completion token with read {@link NormalizedNode} wrapped in {@link Optional} instance
260      */
261     public ListenableFuture<Optional<NormalizedNode>> getConfigRunningData(
262             final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath,
263             final List<YangInstanceIdentifier> fields) {
264         if (fields.isEmpty()) {
265             // RFC doesn't allow to build subtree filter that would expect just empty element in response
266             return Futures.immediateFailedFuture(new IllegalArgumentException(
267                 "Failed to build NETCONF GET-CONFIG RPC: provided list of fields is empty; filter path: "
268                     + filterPath));
269         }
270         final ListenableFuture<? extends DOMRpcResult> response = getConfigRunning(callback, filterPath, fields);
271         return extractData(filterPath, response);
272     }
273
274     /**
275      * Calling GET RPC with subtree filter that is specified by {@link YangInstanceIdentifier}.
276      *
277      * @param callback   RPC response callback
278      * @param filterPath path to requested data
279      * @return asynchronous completion token with read {@link NormalizedNode} wrapped in {@link Optional} instance
280      */
281     public ListenableFuture<Optional<NormalizedNode>> getData(final FutureCallback<DOMRpcResult> callback,
282                                                               final Optional<YangInstanceIdentifier> filterPath) {
283         return extractData(filterPath, get(callback, filterPath));
284     }
285
286     /**
287      * Calling GET RPC with subtree filter tha tis specified by parent {@link YangInstanceIdentifier} and list
288      * of specific fields that caller would like to read. Field paths are relative to parent path.
289      *
290      * @param callback   RPC response callback
291      * @param filterPath parent path to requested data
292      * @param fields     paths to specific fields that are selected under parent path
293      * @return asynchronous completion token with read {@link NormalizedNode} wrapped in {@link Optional} instance
294      */
295     public ListenableFuture<Optional<NormalizedNode>> getData(final FutureCallback<DOMRpcResult> callback,
296             final Optional<YangInstanceIdentifier> filterPath, final List<YangInstanceIdentifier> fields) {
297         if (fields.isEmpty()) {
298             // RFC doesn't allow to build subtree filter that would expect just empty element in response
299             return Futures.immediateFailedFuture(new IllegalArgumentException(
300                     "Failed to build NETCONF GET RPC: provided list of fields is empty; filter path: " + filterPath));
301         }
302         final ListenableFuture<? extends DOMRpcResult> response = get(callback, filterPath, fields);
303         return extractData(filterPath, response);
304     }
305
306     private ListenableFuture<Optional<NormalizedNode>> extractData(final Optional<YangInstanceIdentifier> path,
307             final ListenableFuture<? extends DOMRpcResult> configRunning) {
308         return Futures.transform(configRunning, result -> {
309             checkArgument(result.getErrors().isEmpty(), "Unable to read data: %s, errors: %s", path,
310                 result.getErrors());
311             final DataContainerChild dataNode = ((ContainerNode) result.getResult())
312                     .findChildByArg(NetconfMessageTransformUtil.NETCONF_DATA_NODEID).get();
313             return transformer.selectFromDataStructure(dataNode, path.get());
314         }, MoreExecutors.directExecutor());
315     }
316
317     public ListenableFuture<? extends DOMRpcResult> getConfigRunning(final FutureCallback<DOMRpcResult> callback,
318             final Optional<YangInstanceIdentifier> filterPath) {
319         return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath);
320     }
321
322     private ListenableFuture<? extends DOMRpcResult> getConfigRunning(final FutureCallback<DOMRpcResult> callback,
323             final Optional<YangInstanceIdentifier> filterPath, final List<YangInstanceIdentifier> fields) {
324         return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath, fields);
325     }
326
327     public ListenableFuture<? extends DOMRpcResult> getConfigCandidate(final FutureCallback<DOMRpcResult> callback,
328             final Optional<YangInstanceIdentifier> filterPath) {
329         return getConfig(callback, NETCONF_CANDIDATE_QNAME, filterPath);
330     }
331
332     public ListenableFuture<? extends DOMRpcResult> get(final FutureCallback<DOMRpcResult> callback,
333                                                         final Optional<YangInstanceIdentifier> filterPath) {
334         requireNonNull(callback);
335
336         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_GET_QNAME,
337                 isFilterPresent(filterPath)
338                     ? NetconfMessageTransformUtil.wrap(NETCONF_GET_NODEID,
339                         toFilterStructure(filterPath.get(), mountContext.getEffectiveModelContext()))
340                     : NetconfMessageTransformUtil.GET_RPC_CONTENT);
341         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
342         return future;
343     }
344
345     private ListenableFuture<? extends DOMRpcResult> get(final FutureCallback<DOMRpcResult> callback,
346             final Optional<YangInstanceIdentifier> filterPath, final List<YangInstanceIdentifier> fields) {
347         requireNonNull(callback);
348
349         final NormalizedNode rpcInput;
350         if (isFilterPresent(filterPath)) {
351             rpcInput = NetconfMessageTransformUtil.wrap(NETCONF_GET_NODEID, transformer.toFilterStructure(
352                     Collections.singletonList(FieldsFilter.of(filterPath.get(), fields))));
353         } else if (containsEmptyPath(fields)) {
354             rpcInput = GET_RPC_CONTENT;
355         } else {
356             final DataContainerChild subtreeFilter = getSubtreeFilterFromRootFields(fields);
357             rpcInput = NetconfMessageTransformUtil.wrap(NETCONF_GET_NODEID, subtreeFilter);
358         }
359         final ListenableFuture<? extends DOMRpcResult> response = rpc.invokeRpc(NETCONF_GET_QNAME, rpcInput);
360         Futures.addCallback(response, callback, MoreExecutors.directExecutor());
361         return response;
362     }
363
364     private static boolean containsEmptyPath(final List<YangInstanceIdentifier> fields) {
365         return fields.stream().anyMatch(YangInstanceIdentifier::isEmpty);
366     }
367
368     private DataContainerChild getSubtreeFilterFromRootFields(final List<YangInstanceIdentifier> fields) {
369         final Map<YangInstanceIdentifier, List<YangInstanceIdentifier>> getConfigEntries = fields.stream()
370                 .map(fieldPath -> {
371                     final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(
372                             Iterables.limit(fieldPath.getPathArguments(), 1));
373                     final YangInstanceIdentifier updatedFieldPath = YangInstanceIdentifier.create(
374                             Iterables.skip(fieldPath.getPathArguments(), 1));
375                     return new SimpleEntry<>(rootPath, updatedFieldPath);
376                 })
377                 .collect(Collectors.groupingBy(SimpleEntry::getKey,
378                         Collectors.mapping(SimpleEntry::getValue, Collectors.toList())));
379         final List<FieldsFilter> fieldsFilters = getConfigEntries.keySet().stream()
380                 .map(rootPath -> FieldsFilter.of(rootPath, getConfigEntries.get(rootPath)))
381                 .collect(Collectors.toList());
382         return transformer.toFilterStructure(fieldsFilters);
383     }
384
385     private static boolean isFilterPresent(final Optional<YangInstanceIdentifier> filterPath) {
386         return filterPath.isPresent() && !filterPath.get().isEmpty();
387     }
388
389     public ListenableFuture<? extends DOMRpcResult> editConfigCandidate(
390             final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild editStructure,
391             final ModifyAction modifyAction, final boolean rollback) {
392         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.of(modifyAction), rollback);
393     }
394
395     public ListenableFuture<? extends DOMRpcResult> editConfigCandidate(
396             final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild editStructure,
397             final boolean rollback) {
398         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.empty(), rollback);
399     }
400
401     public ListenableFuture<? extends DOMRpcResult> editConfigRunning(
402             final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild editStructure,
403             final ModifyAction modifyAction, final boolean rollback) {
404         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.of(modifyAction), rollback);
405     }
406
407     public ListenableFuture<? extends DOMRpcResult> editConfigRunning(
408             final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild editStructure,
409             final boolean rollback) {
410         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.empty(), rollback);
411     }
412
413     public ListenableFuture<? extends DOMRpcResult> editConfig(
414             final FutureCallback<? super DOMRpcResult> callback, final QName datastore,
415             final DataContainerChild editStructure, final Optional<ModifyAction> modifyAction,
416             final boolean rollback) {
417         requireNonNull(callback);
418
419         final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_EDIT_CONFIG_QNAME,
420                 getEditConfigContent(requireNonNull(datastore), requireNonNull(editStructure), modifyAction, rollback));
421
422         Futures.addCallback(future, callback, MoreExecutors.directExecutor());
423         return future;
424     }
425
426     public ChoiceNode createEditConfigStrcture(final Optional<NormalizedNode> lastChild,
427                                                final Optional<ModifyAction> operation,
428                                                final YangInstanceIdentifier dataPath) {
429         return Builders.choiceBuilder()
430                 .withNodeIdentifier(EDIT_CONTENT_NODEID)
431                 .withChild(transformer.createEditConfigStructure(lastChild, dataPath, operation))
432                 .build();
433     }
434
435     private static ContainerNode getEditConfigContent(final QName datastore, final DataContainerChild editStructure,
436             final Optional<ModifyAction> defaultOperation, final boolean rollback) {
437         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> editBuilder =
438                 Builders.containerBuilder().withNodeIdentifier(NETCONF_EDIT_CONFIG_NODEID);
439
440         // Target
441         editBuilder.withChild(getTargetNode(datastore));
442
443         // Default operation
444         if (defaultOperation.isPresent()) {
445             final String opString = defaultOperation.get().name().toLowerCase(Locale.ROOT);
446             editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(NETCONF_DEFAULT_OPERATION_NODEID)
447                     .withValue(opString).build());
448         }
449
450         // Error option
451         if (rollback) {
452             editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(NETCONF_ERROR_OPTION_NODEID)
453                     .withValue(ROLLBACK_ON_ERROR_OPTION).build());
454         }
455
456         // Edit content
457         editBuilder.withChild(editStructure);
458         return editBuilder.build();
459     }
460
461     public static @NonNull ContainerNode getSourceNode(final QName datastore) {
462         return Builders.containerBuilder()
463                 .withNodeIdentifier(NETCONF_SOURCE_NODEID)
464                 .withChild(Builders.choiceBuilder()
465                     .withNodeIdentifier(CONFIG_SOURCE_NODEID)
466                     .withChild(ImmutableNodes.leafNode(datastore, Empty.getInstance()))
467                     .build())
468                 .build();
469     }
470
471     public static @NonNull ContainerNode getLockContent(final QName datastore) {
472         return Builders.containerBuilder().withNodeIdentifier(NETCONF_LOCK_NODEID)
473                 .withChild(getTargetNode(datastore)).build();
474     }
475
476     public static @NonNull ContainerNode getTargetNode(final QName datastore) {
477         return Builders.containerBuilder().withNodeIdentifier(NETCONF_TARGET_NODEID)
478                 .withChild(Builders.choiceBuilder().withNodeIdentifier(CONFIG_TARGET_NODEID).withChild(
479                     Builders.leafBuilder().withNodeIdentifier(toId(datastore)).withValue(Empty.getInstance()).build())
480                     .build()).build();
481     }
482
483     public static @NonNull ContainerNode getCopyConfigContent(final QName source, final QName target) {
484         return Builders.containerBuilder().withNodeIdentifier(NETCONF_COPY_CONFIG_NODEID)
485                 .withChild(getTargetNode(target)).withChild(getSourceNode(source)).build();
486     }
487
488     public static @NonNull ContainerNode getValidateContent(final QName source) {
489         return Builders.containerBuilder().withNodeIdentifier(NETCONF_VALIDATE_NODEID)
490                 .withChild(getSourceNode(source)).build();
491     }
492
493     public static @NonNull ContainerNode getUnLockContent(final QName datastore) {
494         return Builders.containerBuilder().withNodeIdentifier(NETCONF_UNLOCK_NODEID)
495                 .withChild(getTargetNode(datastore)).build();
496     }
497 }