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