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