BUG-2632 Netconf connector (optionally) reconnects after a schema change is detected
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / util / NetconfMessageTransformUtil.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.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Lists;
18 import com.google.common.collect.Sets;
19 import java.net.URI;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import javax.annotation.Nullable;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
27 import org.opendaylight.controller.netconf.api.NetconfMessage;
28 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
37 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
38 import org.opendaylight.yangtools.yang.data.api.Node;
39 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
42 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
43 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
44 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
45 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
46 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
47 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52
53 public class NetconfMessageTransformUtil {
54
55     public static final String MESSAGE_ID_ATTR = "message-id";
56     public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.cachedReference(QName.create(CreateSubscriptionInput.QNAME, "create-subscription"));
57
58     private NetconfMessageTransformUtil() {}
59
60     public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring");
61     public static final QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = QName.create(IETF_NETCONF_MONITORING, "format");
62     public static final QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = QName.create(IETF_NETCONF_MONITORING, "location");
63     public static final QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = QName.create(IETF_NETCONF_MONITORING, "identifier");
64     public static final QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = QName.create(IETF_NETCONF_MONITORING, "version");
65     public static final QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = QName.create(IETF_NETCONF_MONITORING, "namespace");
66
67     public static final QName IETF_NETCONF_NOTIFICATIONS = QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications");
68
69     public static URI NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0");
70     public static QName NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
71     public static QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
72     public static QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
73     public static QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option");
74     public static QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
75     public static QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
76     public static QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
77     public static QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
78     public static QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
79     public static QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
80     public static QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate");
81     public static QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config");
82     public static QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation");
83     public static QName NETCONF_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-operation");
84     public static QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
85     public static QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
86     public static QName NETCONF_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes");
87     public static QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
88     public static QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
89     public static QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
90     public static QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
91
92     public static URI NETCONF_ROLLBACK_ON_ERROR_URI = URI
93             .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0");
94     public static String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error";
95
96     public static URI NETCONF_CANDIDATE_URI = URI
97             .create("urn:ietf:params:netconf:capability:candidate:1.0");
98
99     public static URI NETCONF_NOTIFICATONS_URI = URI
100             .create("urn:ietf:params:netconf:capability:notification:1.0");
101
102     public static URI NETCONF_RUNNING_WRITABLE_URI = URI
103             .create("urn:ietf:params:netconf:capability:writable-running:1.0");
104
105     public static QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock");
106     public static QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock");
107
108     // Discard changes message
109     public static final CompositeNode DISCARD_CHANGES_RPC_CONTENT =
110             NodeFactory.createImmutableCompositeNode(NETCONF_DISCARD_CHANGES_QNAME, null, Collections.<Node<?>>emptyList());
111
112     // Commit changes message
113     public static final CompositeNode COMMIT_RPC_CONTENT =
114             NodeFactory.createImmutableCompositeNode(NETCONF_COMMIT_QNAME, null, Collections.<Node<?>>emptyList());
115
116     // Create-subscription changes message
117     public static final CompositeNode CREATE_SUBSCRIPTION_RPC_CONTENT =
118             NodeFactory.createImmutableCompositeNode(CREATE_SUBSCRIPTION_RPC_QNAME, null, Collections.<Node<?>>emptyList());
119
120     public static Node<?> toFilterStructure(final YangInstanceIdentifier identifier) {
121         Node<?> previous = null;
122         if (Iterables.isEmpty(identifier.getPathArguments())) {
123             return null;
124         }
125
126         for (final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument component : identifier.getReversePathArguments()) {
127             if (component instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
128                 previous = toNode((YangInstanceIdentifier.NodeIdentifierWithPredicates)component, previous);
129             } else {
130                 previous = toNode(component, previous);
131             }
132         }
133         return filter("subtree", previous);
134     }
135
136     static Node<?> toNode(final YangInstanceIdentifier.NodeIdentifierWithPredicates argument, final Node<?> node) {
137         final List<Node<?>> list = new ArrayList<>();
138         for (final Map.Entry<QName, Object> arg : argument.getKeyValues().entrySet()) {
139             list.add(new SimpleNodeTOImpl(arg.getKey(), null, arg.getValue()));
140         }
141         if (node != null) {
142             list.add(node);
143         }
144         return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
145     }
146
147     public static void checkValidReply(final NetconfMessage input, final NetconfMessage output)
148             throws NetconfDocumentedException {
149         final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
150         final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
151
152         if(inputMsgId.equals(outputMsgId) == false) {
153             Map<String,String> errorInfo = ImmutableMap.<String,String>builder()
154                     .put( "actual-message-id", outputMsgId )
155                     .put( "expected-message-id", inputMsgId )
156                     .build();
157
158             throw new NetconfDocumentedException( "Response message contained unknown \"message-id\"",
159                     null, NetconfDocumentedException.ErrorType.protocol,
160                     NetconfDocumentedException.ErrorTag.bad_attribute,
161                     NetconfDocumentedException.ErrorSeverity.error, errorInfo );
162         }
163     }
164
165     public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException {
166         if(NetconfMessageUtil.isErrorMessage(output)) {
167             throw NetconfDocumentedException.fromXMLDocument( output.getDocument() );
168         }
169     }
170
171     public static RpcError toRpcError( final NetconfDocumentedException ex )
172     {
173         StringBuilder infoBuilder = new StringBuilder();
174         Map<String, String> errorInfo = ex.getErrorInfo();
175         if( errorInfo != null )
176         {
177             for( Entry<String,String> e: errorInfo.entrySet() ) {
178                 infoBuilder.append( '<' ).append( e.getKey() ).append( '>' ).append( e.getValue() )
179                 .append( "</" ).append( e.getKey() ).append( '>' );
180
181             }
182         }
183
184         ErrorSeverity severity = toRpcErrorSeverity( ex.getErrorSeverity() );
185         return severity == ErrorSeverity.ERROR ?
186                 RpcResultBuilder.newError(
187                         toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
188                         ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() ) :
189                             RpcResultBuilder.newWarning(
190                                     toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
191                                     ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() );
192     }
193
194     private static ErrorSeverity toRpcErrorSeverity( final NetconfDocumentedException.ErrorSeverity severity ) {
195         switch( severity ) {
196         case warning:
197             return RpcError.ErrorSeverity.WARNING;
198         default:
199             return RpcError.ErrorSeverity.ERROR;
200         }
201     }
202
203     private static RpcError.ErrorType toRpcErrorType( final NetconfDocumentedException.ErrorType type )
204     {
205         switch( type ) {
206         case protocol:
207             return RpcError.ErrorType.PROTOCOL;
208         case rpc:
209             return RpcError.ErrorType.RPC;
210         case transport:
211             return RpcError.ErrorType.TRANSPORT;
212         default:
213             return RpcError.ErrorType.APPLICATION;
214         }
215     }
216
217     public static CompositeNode flattenInput(final CompositeNode node) {
218         final QName inputQName = QName.create(node.getNodeType(), "input");
219         final CompositeNode input = node.getFirstCompositeByName(inputQName);
220         if (input == null) {
221             return node;
222         }
223         if (input instanceof CompositeNode) {
224
225             final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
226                     .addAll(input.getValue()) //
227                     .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
228                         @Override
229                         public boolean apply(@Nullable final Node<?> input) {
230                             return !inputQName.equals(input.getNodeType());
231                         }
232                     })) //
233                     .build();
234
235             return ImmutableCompositeNode.create(node.getNodeType(), nodes);
236         }
237
238         return input;
239     }
240
241     static Node<?> toNode(final YangInstanceIdentifier.PathArgument argument, final Node<?> node) {
242         if (node != null) {
243             return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
244         } else {
245             return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
246         }
247     }
248
249     public static Element getDataSubtree(final Document doc) {
250         return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
251     }
252
253     public static boolean isDataRetrievalOperation(final QName rpc) {
254         return NETCONF_URI.equals(rpc.getNamespace())
255                 && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals(
256                         NETCONF_GET_QNAME.getLocalName()));
257     }
258
259     public static boolean isGetOperation(final QName rpc) {
260         return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_QNAME.getLocalName());
261     }
262
263     public static boolean isGetConfigOperation(final QName rpc) {
264         return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName());
265     }
266
267     public static boolean isDataEditOperation(final QName rpc) {
268         return NETCONF_URI.equals(rpc.getNamespace())
269                 && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName());
270     }
271
272     /**
273      * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
274      * <pre>
275      * {@code
276      * rpc
277      *   edit-config
278      *     config
279      *         // All schema nodes from remote schema
280      *     config
281      *   edit-config
282      * rpc
283      * }
284      * </pre>
285      *
286      * This makes the translation of rpc edit-config request(especially the config node)
287      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
288      */
289     public static DataNodeContainer createSchemaForEdit(final SchemaContext schemaContext) {
290         final QName config = QName.create(NETCONF_EDIT_CONFIG_QNAME, "config");
291         final QName editConfig = QName.create(NETCONF_EDIT_CONFIG_QNAME, "edit-config");
292         final NodeContainerProxy configProxy = new NodeContainerProxy(config, schemaContext.getChildNodes());
293         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(editConfig, Sets.<DataSchemaNode>newHashSet(configProxy));
294         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
295     }
296
297     /**
298      * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
299      * <pre>
300      * {@code
301      * rpc
302      *   get
303      *     filter
304      *         // All schema nodes from remote schema
305      *     filter
306      *   get
307      * rpc
308      * }
309      * </pre>
310      *
311      * This makes the translation of rpc get request(especially the config node)
312      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
313      */
314     public static DataNodeContainer createSchemaForGet(final SchemaContext schemaContext) {
315         final QName filter = QName.create(NETCONF_GET_QNAME, "filter");
316         final QName get = QName.create(NETCONF_GET_QNAME, "get");
317         final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes());
318         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(get, Sets.<DataSchemaNode>newHashSet(configProxy));
319         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
320     }
321
322     /**
323      * Creates artificial schema node for get rpc. This artificial schema looks like:
324      * <pre>
325      * {@code
326      * rpc
327      *   get-config
328      *     filter
329      *         // All schema nodes from remote schema
330      *     filter
331      *   get-config
332      * rpc
333      * }
334      * </pre>
335      *
336      * This makes the translation of rpc get-config request(especially the config node)
337      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
338      */
339     public static DataNodeContainer createSchemaForGetConfig(final SchemaContext schemaContext) {
340         final QName filter = QName.create(NETCONF_GET_CONFIG_QNAME, "filter");
341         final QName getConfig = QName.create(NETCONF_GET_CONFIG_QNAME, "get-config");
342         final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes());
343         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(getConfig, Sets.<DataSchemaNode>newHashSet(configProxy));
344         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
345     }
346
347
348     public static Optional<RpcDefinition> findSchemaForRpc(final QName rpcName, final SchemaContext schemaContext) {
349         Preconditions.checkNotNull(rpcName);
350         Preconditions.checkNotNull(schemaContext);
351
352         for (final RpcDefinition rpcDefinition : schemaContext.getOperations()) {
353             if(rpcDefinition.getQName().equals(rpcName)) {
354                 return Optional.of(rpcDefinition);
355             }
356         }
357
358         return Optional.absent();
359     }
360
361     /**
362      * Creates artificial schema node for schema defined rpc. This artificial schema looks like:
363      * <pre>
364      * {@code
365      * rpc
366      *   rpc-name
367      *      // All schema nodes from remote schema
368      *   rpc-name
369      * rpc
370      * }
371      * </pre>
372      *
373      * This makes the translation of schema defined rpc request
374      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
375      */
376     public static DataNodeContainer createSchemaForRpc(final RpcDefinition rpcDefinition) {
377         final NodeContainerProxy rpcBodyProxy = new NodeContainerProxy(rpcDefinition.getQName(), rpcDefinition.getInput().getChildNodes());
378         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(rpcBodyProxy));
379     }
380
381     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
382         if (node != null) {
383             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
384         } else {
385             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
386         }
387     }
388
389     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
390         if (node != null) {
391             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
392         } else {
393             return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
394         }
395     }
396
397     static ImmutableCompositeNode filter(final String type, final Node<?> node) {
398         final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder(); //
399         it.setQName(NETCONF_FILTER_QNAME);
400         it.setAttribute(NETCONF_TYPE_QNAME, type);
401         if (node != null) {
402             return it.add(node).toInstance();
403         } else {
404             return it.toInstance();
405         }
406     }
407
408     public static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
409
410         Node<?> current = node;
411         for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
412             if (current instanceof SimpleNode<?>) {
413                 return null;
414             } else if (current instanceof CompositeNode) {
415                 final CompositeNode currentComposite = (CompositeNode) current;
416
417                 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
418                 if (current == null) {
419                     current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
420                 }
421                 if (current == null) {
422                     current = currentComposite.getFirstSimpleByName(arg.getNodeType());
423                 }
424                 if (current == null) {
425                     current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
426                 }
427                 if (current == null) {
428                     return null;
429                 }
430             }
431         }
432         return current;
433     }
434
435     public static String modifyOperationToXmlString(final ModifyAction operation) {
436         return operation.name().toLowerCase();
437     }
438
439
440     public static CompositeNode createEditConfigStructure(final YangInstanceIdentifier dataPath, final Optional<ModifyAction> operation,
441                                                     final Optional<CompositeNode> lastChildOverride) {
442         Preconditions.checkArgument(Iterables.isEmpty(dataPath.getPathArguments()) == false, "Instance identifier with empty path %s", dataPath);
443
444         List<YangInstanceIdentifier.PathArgument> reversedPath = Lists.reverse(dataPath.getPath());
445
446         // Create deepest edit element with expected edit operation
447         CompositeNode previous = getDeepestEditElement(reversedPath.get(0), operation, lastChildOverride);
448
449         // Remove already processed deepest child
450         reversedPath = Lists.newArrayList(reversedPath);
451         reversedPath.remove(0);
452
453         // Create edit structure in reversed order
454         for (final YangInstanceIdentifier.PathArgument arg : reversedPath) {
455             final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
456             builder.setQName(arg.getNodeType());
457
458             addPredicatesToCompositeNodeBuilder(getPredicates(arg), builder);
459
460             builder.add(previous);
461             previous = builder.toInstance();
462         }
463         return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
464     }
465
466     public static void addPredicatesToCompositeNodeBuilder(final Map<QName, Object> predicates, final CompositeNodeBuilder<ImmutableCompositeNode> builder) {
467         for (final Map.Entry<QName, Object> entry : predicates.entrySet()) {
468             builder.addLeaf(entry.getKey(), entry.getValue());
469         }
470     }
471
472     public static Map<QName, Object> getPredicates(final YangInstanceIdentifier.PathArgument arg) {
473         Map<QName, Object> predicates = Collections.emptyMap();
474         if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
475             predicates = ((YangInstanceIdentifier.NodeIdentifierWithPredicates) arg).getKeyValues();
476         }
477         return predicates;
478     }
479
480     public static CompositeNode getDeepestEditElement(final YangInstanceIdentifier.PathArgument arg, final Optional<ModifyAction> operation, final Optional<CompositeNode> lastChildOverride) {
481         final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
482         builder.setQName(arg.getNodeType());
483
484         final Map<QName, Object> predicates = getPredicates(arg);
485         addPredicatesToCompositeNodeBuilder(predicates, builder);
486
487         if (operation.isPresent()) {
488             builder.setAttribute(NETCONF_OPERATION_QNAME, modifyOperationToXmlString(operation.get()));
489         }
490         if (lastChildOverride.isPresent()) {
491             final List<Node<?>> children = lastChildOverride.get().getValue();
492             for(final Node<?> child : children) {
493                 if(!predicates.containsKey(child.getKey())) {
494                     builder.add(child);
495                 }
496             }
497         }
498
499         return builder.toInstance();
500     }
501 }