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