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