e5b2ba25930563e6366b8951ac3b9c2f2b537319
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtil.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.rfc8040.rests.utils;
9
10 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAMS_PATH;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH_PART;
12
13 import com.google.common.collect.Iterables;
14 import com.google.common.primitives.Ints;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.net.URI;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.function.Function;
23 import java.util.stream.Collectors;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.ws.rs.core.UriInfo;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
30 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
31 import org.opendaylight.restconf.common.context.WriterParameters;
32 import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.restconf.common.errors.RestconfError;
35 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
37 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
38 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
39 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter;
40 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
62 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
63 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
64 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
65 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
66 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
67 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
68 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
70 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
71 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
72 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
76 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
77 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
78
79 /**
80  * Util class for read data from data store via transaction.
81  * <ul>
82  * <li>config
83  * <li>state
84  * <li>all (config + state)
85  * </ul>
86  *
87  */
88 public final class ReadDataTransactionUtil {
89
90     private ReadDataTransactionUtil() {
91         throw new UnsupportedOperationException("Util class.");
92     }
93
94     /**
95      * Parse parameters from URI request and check their types and values.
96      *
97      *
98      * @param identifier
99      *             {@link InstanceIdentifierContext}
100      * @param uriInfo
101      *             URI info
102      * @param tagged
103      *             set tagged for {@link WriterParameters}
104      * @return {@link WriterParameters}
105      */
106     @Nonnull
107     public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
108             @Nullable final UriInfo uriInfo, final boolean tagged) {
109         return parseParams(identifier, uriInfo, tagged);
110     }
111
112     /**
113      * Parse parameters from URI request and check their types and values.
114      *
115      *
116      * @param identifier
117      *             {@link InstanceIdentifierContext}
118      * @param uriInfo
119      *             URI info
120      * @return {@link WriterParameters}
121      */
122     @Nonnull
123     public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
124                                                                @Nullable final UriInfo uriInfo) {
125         return parseParams(identifier, uriInfo, false);
126     }
127
128     private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
129             final boolean tagged) {
130         final WriterParametersBuilder builder = new WriterParametersBuilder();
131         builder.setTagged(tagged);
132
133         if (uriInfo == null) {
134             return builder.build();
135         }
136
137         // check only allowed parameters
138         ParametersUtil.checkParametersTypes(
139                 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
140                 uriInfo.getQueryParameters().keySet(),
141                 RestconfDataServiceConstant.ReadData.CONTENT,
142                 RestconfDataServiceConstant.ReadData.DEPTH,
143                 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
144
145         // read parameters from URI or set default values
146         final List<String> content = uriInfo.getQueryParameters().getOrDefault(
147                 RestconfDataServiceConstant.ReadData.CONTENT,
148                 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
149         final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
150                 RestconfDataServiceConstant.ReadData.DEPTH,
151                 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
152         // fields
153         final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
154                 RestconfDataServiceConstant.ReadData.FIELDS,
155                 Collections.emptyList());
156
157         // parameter can be in URI at most once
158         ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
159         ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
160         ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
161
162         // check and set content
163         final String contentValue = content.get(0);
164         if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
165             if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
166                     && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
167                 throw new RestconfDocumentedException(
168                         new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
169                                 "Invalid content parameter: " + contentValue, null,
170                                 "The content parameter value must be either config, nonconfig or all (default)"));
171             }
172         }
173
174         builder.setContent(content.get(0));
175
176         // check and set depth
177         if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
178             final Integer value = Ints.tryParse(depth.get(0));
179
180             if (value == null
181                     || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
182                         && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
183                 throw new RestconfDocumentedException(
184                         new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
185                                 "Invalid depth parameter: " + depth, null,
186                                 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
187             } else {
188                 builder.setDepth(value);
189             }
190         }
191
192         // check and set fields
193         if (!fields.isEmpty()) {
194             builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
195         }
196
197         return builder.build();
198     }
199
200     /**
201      * Read specific type of data from data store via transaction.
202      *
203      * @param valueOfContent
204      *            type of data to read (config, state, all)
205      * @param transactionNode
206      *            {@link TransactionVarsWrapper} - wrapper for variables
207      * @param schemaContext
208      *            schema context
209      * @return {@link NormalizedNode}
210      */
211     @Nullable
212     public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
213             @Nonnull final TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
214         return readData(valueOfContent, transactionNode, null, schemaContext);
215     }
216
217     /**
218      * Read specific type of data from data store via transaction.
219      *
220      * @param valueOfContent
221      *            type of data to read (config, state, all)
222      * @param transactionNode
223      *            {@link TransactionVarsWrapper} - wrapper for variables
224      * @param withDefa
225      *            vaule of with-defaults parameter
226      * @param ctx
227      *            schema context
228      * @return {@link NormalizedNode}
229      */
230     @Nullable
231     public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
232             @Nonnull final TransactionVarsWrapper transactionNode, final String withDefa, final SchemaContext ctx) {
233         switch (valueOfContent) {
234             case RestconfDataServiceConstant.ReadData.CONFIG:
235                 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
236                 if (withDefa == null) {
237                     return readDataViaTransaction(transactionNode);
238                 } else {
239                     return prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
240                             transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
241                 }
242             case RestconfDataServiceConstant.ReadData.NONCONFIG:
243                 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
244                 return readDataViaTransaction(transactionNode);
245
246             case RestconfDataServiceConstant.ReadData.ALL:
247                 return readAllData(transactionNode, withDefa, ctx);
248
249             default:
250                 throw new RestconfDocumentedException(
251                         new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
252                                 "Invalid content parameter: " + valueOfContent, null,
253                                 "The content parameter value must be either config, nonconfig or all (default)"));
254         }
255     }
256
257     /**
258      * Read specific type of data from data store via transaction and if identifier read data from
259      * streams then put streams from actual schema context to datastore.
260      *
261      * @param identifier
262      *             identifier of data to read
263      * @param content
264      *             type of data to read (config, state, all)
265      * @param transactionNode
266      *             {@link TransactionVarsWrapper} - wrapper for variables
267      * @param withDefa
268      *             vaule of with-defaults parameter
269      * @param schemaContextRef
270      *             schema context
271      * @param uriInfo
272      *             uri info
273      * @return {@link NormalizedNode}
274      */
275     public static NormalizedNode<?, ?> readData(final String identifier, final String content,
276                                                 final TransactionVarsWrapper transactionNode, final String withDefa,
277                                                 final SchemaContextRef schemaContextRef, final UriInfo uriInfo) {
278         final SchemaContext schemaContext = schemaContextRef.get();
279         if (identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
280             final DOMDataTreeReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
281             final boolean exist = SubscribeToStreamUtil.checkExist(schemaContext, wTx);
282
283             for (final NotificationDefinition notificationDefinition : schemaContextRef.get().getNotifications()) {
284                 final List<NotificationListenerAdapter> notifiStreamXML =
285                         CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
286                                 NotificationOutputType.XML.getName());
287                 final List<NotificationListenerAdapter> notifiStreamJSON =
288                         CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
289                                 NotificationOutputType.JSON.getName());
290                 for (final NotificationListenerAdapter listener : Iterables.concat(notifiStreamXML, notifiStreamJSON)) {
291                     final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
292                     final NormalizedNode mapToStreams =
293                             RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
294                                     listener.getSchemaPath().getLastComponent(), schemaContext.getNotifications(),
295                                     null, listener.getOutputType(), uri,
296                                     SubscribeToStreamUtil.getMonitoringModule(schemaContext), exist);
297                     SubscribeToStreamUtil.writeDataToDS(schemaContext,
298                             listener.getSchemaPath().getLastComponent().getLocalName(), wTx, exist,
299                             mapToStreams);
300                 }
301             }
302             SubscribeToStreamUtil.submitData(wTx);
303         }
304         return readData(content, transactionNode, withDefa, schemaContext);
305     }
306
307     private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
308             final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
309         boolean trim;
310         switch (withDefa) {
311             case "trim":
312                 trim = true;
313                 break;
314             case "explicit":
315                 trim = false;
316                 break;
317             default:
318                 throw new RestconfDocumentedException("");
319         }
320
321         final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
322         final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
323         if (result instanceof ContainerNode) {
324             final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
325                     Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
326             buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
327             return builder.build();
328         } else {
329             final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
330                     Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
331             buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
332                     ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
333             return builder.build();
334         }
335     }
336
337     private static void buildMapEntryBuilder(
338             final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
339             final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
340             final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
341         for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
342             final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
343             final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
344             if (child instanceof ContainerNode) {
345                 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> childBuilder =
346                         Builders.containerBuilder((ContainerSchemaNode) childSchema);
347                 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
348                 builder.withChild(childBuilder.build());
349             } else if (child instanceof MapNode) {
350                 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
351                         Builders.mapBuilder((ListSchemaNode) childSchema);
352                 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
353                         ((ListSchemaNode) childSchema).getKeyDefinition());
354                 builder.withChild(childBuilder.build());
355             } else if (child instanceof LeafNode) {
356                 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
357                 final Object nodeVal = child.getValue();
358                 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
359                         Builders.leafBuilder((LeafSchemaNode) childSchema);
360                 if (keys.contains(child.getNodeType())) {
361                     leafBuilder.withValue(((LeafNode) child).getValue());
362                     builder.withChild(leafBuilder.build());
363                 } else {
364                     if (trim) {
365                         if (defaultVal == null || !defaultVal.equals(nodeVal)) {
366                             leafBuilder.withValue(((LeafNode) child).getValue());
367                             builder.withChild(leafBuilder.build());
368                         }
369                     } else {
370                         if (defaultVal != null && defaultVal.equals(nodeVal)) {
371                             leafBuilder.withValue(((LeafNode) child).getValue());
372                             builder.withChild(leafBuilder.build());
373                         }
374                     }
375                 }
376             }
377         }
378     }
379
380     private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
381             final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
382             final List<QName> keys) {
383         for (final MapEntryNode mapEntryNode : result.getValue()) {
384             final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
385             final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
386             final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
387                     Builders.mapEntryBuilder((ListSchemaNode) childSchema);
388             buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
389             builder.withChild(mapEntryBuilder.build());
390         }
391     }
392
393     private static void buildCont(final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder,
394             final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
395             final YangInstanceIdentifier actualPath, final boolean trim) {
396         for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
397             final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
398             final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
399             if (child instanceof ContainerNode) {
400                 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builderChild =
401                         Builders.containerBuilder((ContainerSchemaNode) childSchema);
402                 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
403                 builder.withChild(builderChild.build());
404             } else if (child instanceof MapNode) {
405                 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
406                         Builders.mapBuilder((ListSchemaNode) childSchema);
407                 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
408                         ((ListSchemaNode) childSchema).getKeyDefinition());
409                 builder.withChild(childBuilder.build());
410             } else if (child instanceof LeafNode) {
411                 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
412                 final Object nodeVal = child.getValue();
413                 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
414                         Builders.leafBuilder((LeafSchemaNode) childSchema);
415                 if (trim) {
416                     if (defaultVal == null || !defaultVal.equals(nodeVal)) {
417                         leafBuilder.withValue(((LeafNode) child).getValue());
418                         builder.withChild(leafBuilder.build());
419                     }
420                 } else {
421                     if (defaultVal != null && defaultVal.equals(nodeVal)) {
422                         leafBuilder.withValue(((LeafNode) child).getValue());
423                         builder.withChild(leafBuilder.build());
424                     }
425                 }
426             }
427         }
428     }
429
430     /**
431      * If is set specific {@link LogicalDatastoreType} in
432      * {@link TransactionVarsWrapper}, then read this type of data from DS. If
433      * don't, we have to read all data from DS (state + config)
434      *
435      * @param transactionNode
436      *             {@link TransactionVarsWrapper} - wrapper for variables
437      * @return {@link NormalizedNode}
438      */
439     @Nullable
440     private static NormalizedNode<?, ?> readDataViaTransaction(
441             @Nonnull final TransactionVarsWrapper transactionNode) {
442         final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
443         try (DOMDataTreeReadTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
444             final FluentFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = tx.read(
445                 transactionNode.getLogicalDatastoreType(),
446                 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
447             FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
448                 dataFactory);
449         }
450         return dataFactory.build();
451     }
452
453     /**
454      * Read config and state data, then map them.
455      *
456      * @param transactionNode
457      *            {@link TransactionVarsWrapper} - wrapper for variables
458      * @param withDefa
459      *            with-defaults parameter
460      * @param ctx
461      *            schema context
462      * @return {@link NormalizedNode}
463      */
464     @Nullable
465     private static NormalizedNode<?, ?> readAllData(@Nonnull final TransactionVarsWrapper transactionNode,
466             final String withDefa, final SchemaContext ctx) {
467         // PREPARE STATE DATA NODE
468         transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
469         final NormalizedNode<?, ?> stateDataNode = readDataViaTransaction(transactionNode);
470
471         // PREPARE CONFIG DATA NODE
472         transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
473         final NormalizedNode<?, ?> configDataNode;
474         if (withDefa == null) {
475             configDataNode = readDataViaTransaction(transactionNode);
476         } else {
477             configDataNode = prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
478                     transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
479         }
480
481         // if no data exists
482         if (stateDataNode == null && configDataNode == null) {
483             return null;
484         }
485
486         // return config data
487         if (stateDataNode == null) {
488             return configDataNode;
489         }
490
491         // return state data
492         if (configDataNode == null) {
493             return stateDataNode;
494         }
495
496         // merge data from config and state
497         return mergeStateAndConfigData(stateDataNode, configDataNode);
498     }
499
500     /**
501      * Merge state and config data into a single NormalizedNode.
502      *
503      * @param stateDataNode
504      *             data node of state data
505      * @param configDataNode
506      *             data node of config data
507      * @return {@link NormalizedNode}
508      */
509     @Nonnull
510     private static NormalizedNode<?, ?> mergeStateAndConfigData(@Nonnull final NormalizedNode<?, ?> stateDataNode,
511                                                                 @Nonnull final NormalizedNode<?, ?> configDataNode) {
512         validateNodeMerge(stateDataNode, configDataNode);
513         if (configDataNode instanceof RpcDefinition) {
514             return prepareRpcData(configDataNode, stateDataNode);
515         } else {
516             return prepareData(configDataNode, stateDataNode);
517         }
518     }
519
520     /**
521      * Validates whether the two NormalizedNodes can be merged.
522      *
523      * @param stateDataNode
524      *             data node of state data
525      * @param configDataNode
526      *             data node of config data
527      */
528     private static void validateNodeMerge(@Nonnull final NormalizedNode<?, ?> stateDataNode,
529                                           @Nonnull final NormalizedNode<?, ?> configDataNode) {
530         final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
531         final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
532         if (!moduleOfStateData.equals(moduleOfConfigData)) {
533             throw new RestconfDocumentedException("Unable to merge data from different modules.");
534         }
535     }
536
537     /**
538      * Prepare and map data for rpc.
539      *
540      * @param configDataNode
541      *             data node of config data
542      * @param stateDataNode
543      *             data node of state data
544      * @return {@link NormalizedNode}
545      */
546     @Nonnull
547     private static NormalizedNode<?, ?> prepareRpcData(@Nonnull final NormalizedNode<?, ?> configDataNode,
548                                                                 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
549         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
550                 .mapEntryBuilder();
551         mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
552
553         // MAP CONFIG DATA
554         mapRpcDataNode(configDataNode, mapEntryBuilder);
555         // MAP STATE DATA
556         mapRpcDataNode(stateDataNode, mapEntryBuilder);
557
558         return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
559     }
560
561     /**
562      * Map node to map entry builder.
563      *
564      * @param dataNode
565      *             data node
566      * @param mapEntryBuilder
567      *             builder for mapping data
568      */
569     private static void mapRpcDataNode(@Nonnull final NormalizedNode<?, ?> dataNode,
570                                        @Nonnull final DataContainerNodeBuilder<
571                                                NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
572         ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
573     }
574
575     /**
576      * Prepare and map all data from DS.
577      *
578      * @param configDataNode
579      *             data node of config data
580      * @param stateDataNode
581      *             data node of state data
582      * @return {@link NormalizedNode}
583      */
584     @SuppressWarnings("unchecked")
585     @Nonnull
586     private static NormalizedNode<?, ?> prepareData(@Nonnull final NormalizedNode<?, ?> configDataNode,
587                                                              @Nonnull final NormalizedNode<?, ?> stateDataNode) {
588         if (configDataNode instanceof OrderedMapNode) {
589             final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = Builders
590                     .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
591
592             mapValueToBuilder(
593                     ((OrderedMapNode) configDataNode).getValue(), ((OrderedMapNode) stateDataNode).getValue(), builder);
594
595             return builder.build();
596         } else if (configDataNode instanceof MapNode) {
597             final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
598                     .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
599
600             mapValueToBuilder(
601                     ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
602
603             return builder.build();
604         } else if (configDataNode instanceof MapEntryNode) {
605             final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
606                     .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
607
608             mapValueToBuilder(
609                     ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
610
611             return builder.build();
612         } else if (configDataNode instanceof ContainerNode) {
613             final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = Builders
614                     .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
615
616             mapValueToBuilder(
617                     ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
618
619             return builder.build();
620         } else if (configDataNode instanceof AugmentationNode) {
621             final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
622                     .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
623
624             mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
625                     ((AugmentationNode) stateDataNode).getValue(), builder);
626
627             return builder.build();
628         } else if (configDataNode instanceof ChoiceNode) {
629             final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
630                     .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
631
632             mapValueToBuilder(
633                     ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
634
635             return builder.build();
636         } else if (configDataNode instanceof LeafNode) {
637             return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
638         } else if (configDataNode instanceof OrderedLeafSetNode) {
639             final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
640                 .orderedLeafSetBuilder().withNodeIdentifier(((OrderedLeafSetNode<?>) configDataNode).getIdentifier());
641
642             mapValueToBuilder(((OrderedLeafSetNode<Object>) configDataNode).getValue(),
643                     ((OrderedLeafSetNode<Object>) stateDataNode).getValue(), builder);
644             return builder.build();
645         } else if (configDataNode instanceof LeafSetNode) {
646             final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
647                     .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
648
649             mapValueToBuilder(((LeafSetNode<Object>) configDataNode).getValue(),
650                     ((LeafSetNode<Object>) stateDataNode).getValue(), builder);
651             return builder.build();
652         } else if (configDataNode instanceof UnkeyedListNode) {
653             final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
654                     .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
655
656             mapValueToBuilder(((UnkeyedListNode) configDataNode).getValue(),
657                     ((UnkeyedListNode) stateDataNode).getValue(), builder);
658             return builder.build();
659         } else if (configDataNode instanceof UnkeyedListEntryNode) {
660             final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
661                 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
662
663             mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).getValue(),
664                     ((UnkeyedListEntryNode) stateDataNode).getValue(), builder);
665             return builder.build();
666         } else {
667             throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
668         }
669     }
670
671     /**
672      * Map value from container node to builder.
673      *
674      * @param configData
675      *             collection of config data nodes
676      * @param stateData
677      *             collection of state data nodes
678      * @param builder
679      *             builder
680      */
681     private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
682             @Nonnull final Collection<T> configData,
683             @Nonnull final Collection<T> stateData,
684             @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
685         final Map<PathArgument, T> configMap = configData.stream().collect(
686                 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
687         final Map<PathArgument, T> stateMap = stateData.stream().collect(
688                 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
689
690         // merge config and state data of children with different identifiers
691         mapDataToBuilder(configMap, stateMap, builder);
692
693         // merge config and state data of children with the same identifiers
694         mergeDataToBuilder(configMap, stateMap, builder);
695     }
696
697     /**
698      * Map data with different identifiers to builder. Data with different identifiers can be just added
699      * as childs to parent node.
700      *
701      * @param configMap
702      *             map of config data nodes
703      * @param stateMap
704      *             map of state data nodes
705      * @param builder
706      *           - builder
707      */
708     private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
709             @Nonnull final Map<PathArgument, T> configMap,
710             @Nonnull final Map<PathArgument, T> stateMap,
711             @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
712         configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
713             y -> builder.addChild(y.getValue()));
714         stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
715             y -> builder.addChild(y.getValue()));
716     }
717
718     /**
719      * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
720      * go one level down with {@code prepareData} method.
721      *
722      * @param configMap
723      *             immutable config data
724      * @param stateMap
725      *             immutable state data
726      * @param builder
727      *           - builder
728      */
729     @SuppressWarnings("unchecked")
730     private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
731             @Nonnull final Map<PathArgument, T> configMap,
732             @Nonnull final Map<PathArgument, T> stateMap,
733             @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
734         // it is enough to process only config data because operational contains the same data
735         configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
736             y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));
737     }
738 }