Merge "Bug 2697: Improvement wrong response handling, missing message"
[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     // Get message
117     public static final CompositeNode GET_RPC_CONTENT =
118             NodeFactory.createImmutableCompositeNode(NETCONF_GET_QNAME, null, Collections.<Node<?>>emptyList());
119
120     // Create-subscription changes message
121     public static final CompositeNode CREATE_SUBSCRIPTION_RPC_CONTENT =
122             NodeFactory.createImmutableCompositeNode(CREATE_SUBSCRIPTION_RPC_QNAME, null, Collections.<Node<?>>emptyList());
123
124     public static Node<?> toFilterStructure(final YangInstanceIdentifier identifier) {
125         Node<?> previous = null;
126         if (Iterables.isEmpty(identifier.getPathArguments())) {
127             return null;
128         }
129
130         for (final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument component : identifier.getReversePathArguments()) {
131             if (component instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
132                 previous = toNode((YangInstanceIdentifier.NodeIdentifierWithPredicates)component, previous);
133             } else {
134                 previous = toNode(component, previous);
135             }
136         }
137         return filter("subtree", previous);
138     }
139
140     static Node<?> toNode(final YangInstanceIdentifier.NodeIdentifierWithPredicates argument, final Node<?> node) {
141         final List<Node<?>> list = new ArrayList<>();
142         for (final Map.Entry<QName, Object> arg : argument.getKeyValues().entrySet()) {
143             list.add(new SimpleNodeTOImpl(arg.getKey(), null, arg.getValue()));
144         }
145         if (node != null) {
146             list.add(node);
147         }
148         return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
149     }
150
151     public static void checkValidReply(final NetconfMessage input, final NetconfMessage output)
152             throws NetconfDocumentedException {
153         final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
154         final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR);
155
156         if(inputMsgId.equals(outputMsgId) == false) {
157             Map<String,String> errorInfo = ImmutableMap.<String,String>builder()
158                     .put( "actual-message-id", outputMsgId )
159                     .put( "expected-message-id", inputMsgId )
160                     .build();
161
162             throw new NetconfDocumentedException( "Response message contained unknown \"message-id\"",
163                     null, NetconfDocumentedException.ErrorType.protocol,
164                     NetconfDocumentedException.ErrorTag.bad_attribute,
165                     NetconfDocumentedException.ErrorSeverity.error, errorInfo );
166         }
167     }
168
169     public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException {
170         if(NetconfMessageUtil.isErrorMessage(output)) {
171             throw NetconfDocumentedException.fromXMLDocument( output.getDocument() );
172         }
173     }
174
175     public static RpcError toRpcError( final NetconfDocumentedException ex )
176     {
177         StringBuilder infoBuilder = new StringBuilder();
178         Map<String, String> errorInfo = ex.getErrorInfo();
179         if( errorInfo != null )
180         {
181             for( Entry<String,String> e: errorInfo.entrySet() ) {
182                 infoBuilder.append( '<' ).append( e.getKey() ).append( '>' ).append( e.getValue() )
183                 .append( "</" ).append( e.getKey() ).append( '>' );
184
185             }
186         }
187
188         ErrorSeverity severity = toRpcErrorSeverity( ex.getErrorSeverity() );
189         return severity == ErrorSeverity.ERROR ?
190                 RpcResultBuilder.newError(
191                         toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
192                         ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() ) :
193                             RpcResultBuilder.newWarning(
194                                     toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(),
195                                     ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() );
196     }
197
198     private static ErrorSeverity toRpcErrorSeverity( final NetconfDocumentedException.ErrorSeverity severity ) {
199         switch( severity ) {
200         case warning:
201             return RpcError.ErrorSeverity.WARNING;
202         default:
203             return RpcError.ErrorSeverity.ERROR;
204         }
205     }
206
207     private static RpcError.ErrorType toRpcErrorType( final NetconfDocumentedException.ErrorType type )
208     {
209         switch( type ) {
210         case protocol:
211             return RpcError.ErrorType.PROTOCOL;
212         case rpc:
213             return RpcError.ErrorType.RPC;
214         case transport:
215             return RpcError.ErrorType.TRANSPORT;
216         default:
217             return RpcError.ErrorType.APPLICATION;
218         }
219     }
220
221     public static CompositeNode flattenInput(final CompositeNode node) {
222         final QName inputQName = QName.create(node.getNodeType(), "input");
223         final CompositeNode input = node.getFirstCompositeByName(inputQName);
224         if (input == null) {
225             return node;
226         }
227         if (input instanceof CompositeNode) {
228
229             final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
230                     .addAll(input.getValue()) //
231                     .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
232                         @Override
233                         public boolean apply(@Nullable final Node<?> input) {
234                             return !inputQName.equals(input.getNodeType());
235                         }
236                     })) //
237                     .build();
238
239             return ImmutableCompositeNode.create(node.getNodeType(), nodes);
240         }
241
242         return input;
243     }
244
245     static Node<?> toNode(final YangInstanceIdentifier.PathArgument argument, final Node<?> node) {
246         if (node != null) {
247             return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
248         } else {
249             return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
250         }
251     }
252
253     public static Element getDataSubtree(final Document doc) {
254         return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
255     }
256
257     public static boolean isDataRetrievalOperation(final QName rpc) {
258         return NETCONF_URI.equals(rpc.getNamespace())
259                 && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals(
260                         NETCONF_GET_QNAME.getLocalName()));
261     }
262
263     public static boolean isGetOperation(final QName rpc) {
264         return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_QNAME.getLocalName());
265     }
266
267     public static boolean isGetConfigOperation(final QName rpc) {
268         return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName());
269     }
270
271     public static boolean isDataEditOperation(final QName rpc) {
272         return NETCONF_URI.equals(rpc.getNamespace())
273                 && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName());
274     }
275
276     /**
277      * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
278      * <pre>
279      * {@code
280      * rpc
281      *   edit-config
282      *     config
283      *         // All schema nodes from remote schema
284      *     config
285      *   edit-config
286      * rpc
287      * }
288      * </pre>
289      *
290      * This makes the translation of rpc edit-config request(especially the config node)
291      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
292      */
293     public static DataNodeContainer createSchemaForEdit(final SchemaContext schemaContext) {
294         final QName config = QName.create(NETCONF_EDIT_CONFIG_QNAME, "config");
295         final QName editConfig = QName.create(NETCONF_EDIT_CONFIG_QNAME, "edit-config");
296         final NodeContainerProxy configProxy = new NodeContainerProxy(config, schemaContext.getChildNodes());
297         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(editConfig, Sets.<DataSchemaNode>newHashSet(configProxy));
298         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
299     }
300
301     /**
302      * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
303      * <pre>
304      * {@code
305      * rpc
306      *   get
307      *     filter
308      *         // All schema nodes from remote schema
309      *     filter
310      *   get
311      * rpc
312      * }
313      * </pre>
314      *
315      * This makes the translation of rpc get request(especially the config node)
316      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
317      */
318     public static DataNodeContainer createSchemaForGet(final SchemaContext schemaContext) {
319         final QName filter = QName.create(NETCONF_GET_QNAME, "filter");
320         final QName get = QName.create(NETCONF_GET_QNAME, "get");
321         final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes());
322         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(get, Sets.<DataSchemaNode>newHashSet(configProxy));
323         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
324     }
325
326     /**
327      * Creates artificial schema node for get rpc. This artificial schema looks like:
328      * <pre>
329      * {@code
330      * rpc
331      *   get-config
332      *     filter
333      *         // All schema nodes from remote schema
334      *     filter
335      *   get-config
336      * rpc
337      * }
338      * </pre>
339      *
340      * This makes the translation of rpc get-config request(especially the config node)
341      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
342      */
343     public static DataNodeContainer createSchemaForGetConfig(final SchemaContext schemaContext) {
344         final QName filter = QName.create(NETCONF_GET_CONFIG_QNAME, "filter");
345         final QName getConfig = QName.create(NETCONF_GET_CONFIG_QNAME, "get-config");
346         final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes());
347         final NodeContainerProxy editConfigProxy = new NodeContainerProxy(getConfig, Sets.<DataSchemaNode>newHashSet(configProxy));
348         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
349     }
350
351
352     public static Optional<RpcDefinition> findSchemaForRpc(final QName rpcName, final SchemaContext schemaContext) {
353         Preconditions.checkNotNull(rpcName);
354         Preconditions.checkNotNull(schemaContext);
355
356         for (final RpcDefinition rpcDefinition : schemaContext.getOperations()) {
357             if(rpcDefinition.getQName().equals(rpcName)) {
358                 return Optional.of(rpcDefinition);
359             }
360         }
361
362         return Optional.absent();
363     }
364
365     /**
366      * Creates artificial schema node for schema defined rpc. This artificial schema looks like:
367      * <pre>
368      * {@code
369      * rpc
370      *   rpc-name
371      *      // All schema nodes from remote schema
372      *   rpc-name
373      * rpc
374      * }
375      * </pre>
376      *
377      * This makes the translation of schema defined rpc request
378      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
379      */
380     public static DataNodeContainer createSchemaForRpc(final RpcDefinition rpcDefinition) {
381         final NodeContainerProxy rpcBodyProxy = new NodeContainerProxy(rpcDefinition.getQName(), rpcDefinition.getInput().getChildNodes());
382         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(rpcBodyProxy));
383     }
384
385     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
386         if (node != null) {
387             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
388         } else {
389             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
390         }
391     }
392
393     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
394         if (node != null) {
395             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
396         } else {
397             return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
398         }
399     }
400
401     static ImmutableCompositeNode filter(final String type, final Node<?> node) {
402         final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder(); //
403         it.setQName(NETCONF_FILTER_QNAME);
404         it.setAttribute(NETCONF_TYPE_QNAME, type);
405         if (node != null) {
406             return it.add(node).toInstance();
407         } else {
408             return it.toInstance();
409         }
410     }
411
412     public static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
413
414         Node<?> current = node;
415         for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
416             if (current instanceof SimpleNode<?>) {
417                 return null;
418             } else if (current instanceof CompositeNode) {
419                 final CompositeNode currentComposite = (CompositeNode) current;
420
421                 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
422                 if (current == null) {
423                     current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
424                 }
425                 if (current == null) {
426                     current = currentComposite.getFirstSimpleByName(arg.getNodeType());
427                 }
428                 if (current == null) {
429                     current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
430                 }
431                 if (current == null) {
432                     return null;
433                 }
434             }
435         }
436         return current;
437     }
438
439     public static String modifyOperationToXmlString(final ModifyAction operation) {
440         return operation.name().toLowerCase();
441     }
442
443
444     public static CompositeNode createEditConfigStructure(final YangInstanceIdentifier dataPath, final Optional<ModifyAction> operation,
445                                                     final Optional<CompositeNode> lastChildOverride) {
446         Preconditions.checkArgument(Iterables.isEmpty(dataPath.getPathArguments()) == false, "Instance identifier with empty path %s", dataPath);
447
448         List<YangInstanceIdentifier.PathArgument> reversedPath = Lists.reverse(dataPath.getPath());
449
450         // Create deepest edit element with expected edit operation
451         CompositeNode previous = getDeepestEditElement(reversedPath.get(0), operation, lastChildOverride);
452
453         // Remove already processed deepest child
454         reversedPath = Lists.newArrayList(reversedPath);
455         reversedPath.remove(0);
456
457         // Create edit structure in reversed order
458         for (final YangInstanceIdentifier.PathArgument arg : reversedPath) {
459             final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
460             builder.setQName(arg.getNodeType());
461
462             addPredicatesToCompositeNodeBuilder(getPredicates(arg), builder);
463
464             builder.add(previous);
465             previous = builder.toInstance();
466         }
467         return ImmutableCompositeNode.create(NETCONF_CONFIG_QNAME, ImmutableList.<Node<?>>of(previous));
468     }
469
470     public static void addPredicatesToCompositeNodeBuilder(final Map<QName, Object> predicates, final CompositeNodeBuilder<ImmutableCompositeNode> builder) {
471         for (final Map.Entry<QName, Object> entry : predicates.entrySet()) {
472             builder.addLeaf(entry.getKey(), entry.getValue());
473         }
474     }
475
476     public static Map<QName, Object> getPredicates(final YangInstanceIdentifier.PathArgument arg) {
477         Map<QName, Object> predicates = Collections.emptyMap();
478         if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
479             predicates = ((YangInstanceIdentifier.NodeIdentifierWithPredicates) arg).getKeyValues();
480         }
481         return predicates;
482     }
483
484     public static CompositeNode getDeepestEditElement(final YangInstanceIdentifier.PathArgument arg, final Optional<ModifyAction> operation, final Optional<CompositeNode> lastChildOverride) {
485         final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
486         builder.setQName(arg.getNodeType());
487
488         final Map<QName, Object> predicates = getPredicates(arg);
489         addPredicatesToCompositeNodeBuilder(predicates, builder);
490
491         if (operation.isPresent()) {
492             builder.setAttribute(NETCONF_OPERATION_QNAME, modifyOperationToXmlString(operation.get()));
493         }
494         if (lastChildOverride.isPresent()) {
495             final List<Node<?>> children = lastChildOverride.get().getValue();
496             for(final Node<?> child : children) {
497                 if(!predicates.containsKey(child.getKey())) {
498                     builder.add(child);
499                 }
500             }
501         }
502
503         return builder.toInstance();
504     }
505 }