2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collection;
12 import java.util.List;
14 import java.util.Optional;
15 import java.util.function.Function;
16 import java.util.stream.Collectors;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.nb.rfc8040.ContentParam;
23 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
24 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
53 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
54 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
55 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
56 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
58 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
59 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
64 * Util class for read data from data store via transaction.
68 * <li>all (config + state)
71 public final class ReadDataTransactionUtil {
72 private ReadDataTransactionUtil() {
77 * Read specific type of data from data store via transaction. Close {@link DOMTransactionChain} if any
78 * inside of object {@link RestconfStrategy} provided as a parameter.
80 * @param content type of data to read (config, state, all)
81 * @param path the path to read
82 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
83 * @param withDefa value of with-defaults parameter
84 * @param ctx schema context
85 * @return {@link NormalizedNode}
87 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
88 final @NonNull YangInstanceIdentifier path,
89 final @NonNull RestconfStrategy strategy,
90 final WithDefaultsParam withDefa,
91 final EffectiveModelContext ctx) {
92 return switch (content) {
93 case ALL -> readAllData(strategy, path, withDefa, ctx);
95 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
96 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
98 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
103 * Read specific type of data from data store via transaction with specified subtrees that should only be read.
104 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
106 * @param content type of data to read (config, state, all)
107 * @param path the parent path to read
108 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
109 * @param withDefa value of with-defaults parameter
110 * @param ctx schema context
111 * @param fields paths to selected subtrees which should be read, relative to to the parent path
112 * @return {@link NormalizedNode}
114 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
115 final @NonNull YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy,
116 final @Nullable WithDefaultsParam withDefa, @NonNull final EffectiveModelContext ctx,
117 final @NonNull List<YangInstanceIdentifier> fields) {
118 return switch (content) {
119 case ALL -> readAllData(strategy, path, withDefa, ctx, fields);
121 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, fields);
122 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
124 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
128 private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result,
129 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
130 final boolean trim = switch (withDefa) {
132 case EXPLICIT -> false;
133 case REPORT_ALL, REPORT_ALL_TAGGED -> throw new RestconfDocumentedException(
134 "Unsupported with-defaults value " + withDefa.paramValue());
136 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
137 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
138 if (result instanceof ContainerNode) {
139 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder =
140 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
141 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
142 return builder.build();
144 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
145 SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
146 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
147 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
148 return builder.build();
152 private static void buildMapEntryBuilder(
153 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
154 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
155 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
156 for (final DataContainerChild child : result.body()) {
157 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
158 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
159 if (child instanceof ContainerNode) {
160 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> childBuilder =
161 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
162 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
163 builder.withChild(childBuilder.build());
164 } else if (child instanceof MapNode) {
165 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> childBuilder =
166 SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
167 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
168 ((ListSchemaNode) childSchema).getKeyDefinition());
169 builder.withChild(childBuilder.build());
170 } else if (child instanceof LeafNode) {
171 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
172 final Object nodeVal = child.body();
173 if (keys.contains(child.getIdentifier().getNodeType())) {
174 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
176 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
177 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
179 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
180 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
186 private static void buildList(final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder,
187 final MapNode result, final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path,
188 final boolean trim, final List<QName> keys) {
189 for (final MapEntryNode mapEntryNode : result.body()) {
190 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
191 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(actualNode).orElseThrow()
192 .getDataSchemaNode();
193 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
194 SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) childSchema);
195 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
196 builder.withChild(mapEntryBuilder.build());
200 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
201 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
202 final YangInstanceIdentifier actualPath, final boolean trim) {
203 for (final DataContainerChild child : result.body()) {
204 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
205 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
206 if (child instanceof ContainerNode) {
207 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builderChild =
208 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
209 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
210 builder.withChild(builderChild.build());
211 } else if (child instanceof MapNode) {
212 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> childBuilder =
213 SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
214 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
215 ((ListSchemaNode) childSchema).getKeyDefinition());
216 builder.withChild(childBuilder.build());
217 } else if (child instanceof LeafNode) {
218 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
219 final Object nodeVal = child.body();
221 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
222 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
224 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
225 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
232 * If is set specific {@link LogicalDatastoreType} in {@link RestconfStrategy}, then read this type of data from DS.
233 * If don't, we have to read all data from DS (state + config)
235 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
236 * @param closeTransactionChain If is set to true, after transaction it will close transactionChain
237 * in {@link RestconfStrategy} if any
238 * @return {@link NormalizedNode}
240 static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
241 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
242 return extractReadData(strategy, path, strategy.read(store, path));
246 * Read specific type of data {@link LogicalDatastoreType} via transaction in {@link RestconfStrategy} with
247 * specified subtrees that should only be read.
249 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
250 * @param store datastore type
251 * @param path parent path to selected fields
252 * @param closeTransactionChain if it is set to {@code true}, after transaction it will close transactionChain
253 * in {@link RestconfStrategy} if any
254 * @param fields paths to selected subtrees which should be read, relative to to the parent path
255 * @return {@link NormalizedNode}
257 private static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
258 final @NonNull LogicalDatastoreType store, final @NonNull YangInstanceIdentifier path,
259 final @NonNull List<YangInstanceIdentifier> fields) {
260 return extractReadData(strategy, path, strategy.read(store, path, fields));
263 private static NormalizedNode extractReadData(final RestconfStrategy strategy,
264 final YangInstanceIdentifier path, final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
265 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
266 FutureCallbackTx.addCallback(dataFuture, "READ", dataFactory, path);
267 return dataFactory.build();
271 * Read config and state data, then map them. Close {@link DOMTransactionChain} inside of object
272 * {@link RestconfStrategy} provided as a parameter if any.
274 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
275 * @param withDefa with-defaults parameter
276 * @param ctx schema context
277 * @return {@link NormalizedNode}
279 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
280 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
281 // PREPARE STATE DATA NODE
282 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
283 // PREPARE CONFIG DATA NODE
284 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION,
287 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
288 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
292 * Read config and state data with selected subtrees that should only be read, then map them.
293 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
295 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
296 * @param path parent path to selected fields
297 * @param withDefa with-defaults parameter
298 * @param ctx schema context
299 * @param fields paths to selected subtrees which should be read, relative to to the parent path
300 * @return {@link NormalizedNode}
302 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
303 final @NonNull YangInstanceIdentifier path, final @Nullable WithDefaultsParam withDefa,
304 final @NonNull EffectiveModelContext ctx, final @NonNull List<YangInstanceIdentifier> fields) {
305 // PREPARE STATE DATA NODE
306 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path,
309 // PREPARE CONFIG DATA NODE
310 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
312 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
313 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
316 private static NormalizedNode mergeConfigAndSTateDataIfNeeded(final NormalizedNode stateDataNode,
317 final NormalizedNode configDataNode) {
319 if (stateDataNode == null && configDataNode == null) {
323 // return config data
324 if (stateDataNode == null) {
325 return configDataNode;
329 if (configDataNode == null) {
330 return stateDataNode;
333 // merge data from config and state
334 return mergeStateAndConfigData(stateDataNode, configDataNode);
338 * Merge state and config data into a single NormalizedNode.
340 * @param stateDataNode data node of state data
341 * @param configDataNode data node of config data
342 * @return {@link NormalizedNode}
344 private static @NonNull NormalizedNode mergeStateAndConfigData(
345 final @NonNull NormalizedNode stateDataNode, final @NonNull NormalizedNode configDataNode) {
346 validateNodeMerge(stateDataNode, configDataNode);
347 if (configDataNode instanceof RpcDefinition) {
348 return prepareRpcData(configDataNode, stateDataNode);
350 return prepareData(configDataNode, stateDataNode);
355 * Validates whether the two NormalizedNodes can be merged.
357 * @param stateDataNode data node of state data
358 * @param configDataNode data node of config data
360 private static void validateNodeMerge(final @NonNull NormalizedNode stateDataNode,
361 final @NonNull NormalizedNode configDataNode) {
362 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
363 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
364 if (!moduleOfStateData.equals(moduleOfConfigData)) {
365 throw new RestconfDocumentedException("Unable to merge data from different modules.");
370 * Prepare and map data for rpc.
372 * @param configDataNode data node of config data
373 * @param stateDataNode data node of state data
374 * @return {@link NormalizedNode}
376 private static @NonNull NormalizedNode prepareRpcData(final @NonNull NormalizedNode configDataNode,
377 final @NonNull NormalizedNode stateDataNode) {
378 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
380 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
383 mapRpcDataNode(configDataNode, mapEntryBuilder);
385 mapRpcDataNode(stateDataNode, mapEntryBuilder);
387 return ImmutableNodes.mapNodeBuilder(configDataNode.getIdentifier().getNodeType())
388 .addChild(mapEntryBuilder.build())
393 * Map node to map entry builder.
395 * @param dataNode data node
396 * @param mapEntryBuilder builder for mapping data
398 private static void mapRpcDataNode(final @NonNull NormalizedNode dataNode,
399 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
400 ((ContainerNode) dataNode).body().forEach(mapEntryBuilder::addChild);
404 * Prepare and map all data from DS.
406 * @param configDataNode data node of config data
407 * @param stateDataNode data node of state data
408 * @return {@link NormalizedNode}
410 @SuppressWarnings("unchecked")
411 private static @NonNull NormalizedNode prepareData(final @NonNull NormalizedNode configDataNode,
412 final @NonNull NormalizedNode stateDataNode) {
413 if (configDataNode instanceof UserMapNode) {
414 final CollectionNodeBuilder<MapEntryNode, UserMapNode> builder = Builders
415 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
418 ((UserMapNode) configDataNode).body(), ((UserMapNode) stateDataNode).body(), builder);
420 return builder.build();
421 } else if (configDataNode instanceof MapNode) {
422 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder = ImmutableNodes
423 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
426 ((MapNode) configDataNode).body(), ((MapNode) stateDataNode).body(), builder);
428 return builder.build();
429 } else if (configDataNode instanceof MapEntryNode) {
430 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
431 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
434 ((MapEntryNode) configDataNode).body(), ((MapEntryNode) stateDataNode).body(), builder);
436 return builder.build();
437 } else if (configDataNode instanceof ContainerNode) {
438 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
439 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
442 ((ContainerNode) configDataNode).body(), ((ContainerNode) stateDataNode).body(), builder);
444 return builder.build();
445 } else if (configDataNode instanceof AugmentationNode) {
446 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
447 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
449 mapValueToBuilder(((AugmentationNode) configDataNode).body(),
450 ((AugmentationNode) stateDataNode).body(), builder);
452 return builder.build();
453 } else if (configDataNode instanceof ChoiceNode) {
454 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
455 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
458 ((ChoiceNode) configDataNode).body(), ((ChoiceNode) stateDataNode).body(), builder);
460 return builder.build();
461 } else if (configDataNode instanceof LeafNode) {
462 return ImmutableNodes.leafNode(configDataNode.getIdentifier().getNodeType(), configDataNode.body());
463 } else if (configDataNode instanceof UserLeafSetNode) {
464 final ListNodeBuilder<Object, UserLeafSetNode<Object>> builder = Builders
465 .orderedLeafSetBuilder().withNodeIdentifier(((UserLeafSetNode<?>) configDataNode).getIdentifier());
467 mapValueToBuilder(((UserLeafSetNode<Object>) configDataNode).body(),
468 ((UserLeafSetNode<Object>) stateDataNode).body(), builder);
469 return builder.build();
470 } else if (configDataNode instanceof LeafSetNode) {
471 final ListNodeBuilder<Object, SystemLeafSetNode<Object>> builder = Builders
472 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
474 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).body(),
475 ((LeafSetNode<Object>) stateDataNode).body(), builder);
476 return builder.build();
477 } else if (configDataNode instanceof LeafSetEntryNode) {
478 return Builders.leafSetEntryBuilder()
479 .withNodeIdentifier(((LeafSetEntryNode<?>) configDataNode).getIdentifier())
480 .withValue(configDataNode.body())
482 } else if (configDataNode instanceof UnkeyedListNode) {
483 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
484 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
486 mapValueToBuilder(((UnkeyedListNode) configDataNode).body(),
487 ((UnkeyedListNode) stateDataNode).body(), builder);
488 return builder.build();
489 } else if (configDataNode instanceof UnkeyedListEntryNode) {
490 final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
491 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
493 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).body(),
494 ((UnkeyedListEntryNode) stateDataNode).body(), builder);
495 return builder.build();
497 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
502 * Map value from container node to builder.
504 * @param configData collection of config data nodes
505 * @param stateData collection of state data nodes
506 * @param builder builder
508 private static <T extends NormalizedNode> void mapValueToBuilder(
509 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
510 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
511 final Map<PathArgument, T> configMap = configData.stream().collect(
512 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
513 final Map<PathArgument, T> stateMap = stateData.stream().collect(
514 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
516 // merge config and state data of children with different identifiers
517 mapDataToBuilder(configMap, stateMap, builder);
519 // merge config and state data of children with the same identifiers
520 mergeDataToBuilder(configMap, stateMap, builder);
524 * Map data with different identifiers to builder. Data with different identifiers can be just added
525 * as childs to parent node.
527 * @param configMap map of config data nodes
528 * @param stateMap map of state data nodes
529 * @param builder - builder
531 private static <T extends NormalizedNode> void mapDataToBuilder(
532 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
533 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
534 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
535 y -> builder.addChild(y.getValue()));
536 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
537 y -> builder.addChild(y.getValue()));
541 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
542 * go one level down with {@code prepareData} method.
544 * @param configMap immutable config data
545 * @param stateMap immutable state data
546 * @param builder - builder
548 @SuppressWarnings("unchecked")
549 private static <T extends NormalizedNode> void mergeDataToBuilder(
550 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
551 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
552 // it is enough to process only config data because operational contains the same data
553 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
554 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));