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.NoSuchElementException;
15 import java.util.Optional;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.nb.rfc8040.ContentParam;
24 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
25 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
56 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
57 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
58 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
61 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
66 * Util class for read data from data store via transaction.
70 * <li>all (config + state)
73 public final class ReadDataTransactionUtil {
74 private ReadDataTransactionUtil() {
79 * Read specific type of data from data store via transaction. Close {@link DOMTransactionChain} if any
80 * inside of object {@link RestconfStrategy} provided as a parameter.
82 * @param content type of data to read (config, state, all)
83 * @param path the path to read
84 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
85 * @param withDefa value of with-defaults parameter
86 * @param ctx schema context
87 * @return {@link NormalizedNode}
89 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
90 final @NonNull YangInstanceIdentifier path,
91 final @NonNull RestconfStrategy strategy,
92 final WithDefaultsParam withDefa,
93 final EffectiveModelContext ctx) {
94 return switch (content) {
95 case ALL -> readAllData(strategy, path, withDefa, ctx);
97 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
98 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
100 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
105 * Read specific type of data from data store via transaction with specified subtrees that should only be read.
106 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
108 * @param content type of data to read (config, state, all)
109 * @param path the parent path to read
110 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
111 * @param withDefa value of with-defaults parameter
112 * @param ctx schema context
113 * @param fields paths to selected subtrees which should be read, relative to to the parent path
114 * @return {@link NormalizedNode}
116 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
117 final @NonNull YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy,
118 final @Nullable WithDefaultsParam withDefa, @NonNull final EffectiveModelContext ctx,
119 final @NonNull List<YangInstanceIdentifier> fields) {
120 return switch (content) {
121 case ALL -> readAllData(strategy, path, withDefa, ctx, fields);
123 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, fields);
124 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
126 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
130 private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result,
131 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
132 final boolean trim = switch (withDefa) {
134 case EXPLICIT -> false;
135 case REPORT_ALL, REPORT_ALL_TAGGED -> throw new RestconfDocumentedException(
136 "Unsupported with-defaults value " + withDefa.paramValue());
138 final DataSchemaContextNode<?> ctxNode = DataSchemaContextTree.from(ctx).findChild(path).orElseThrow();
139 final DataSchemaNode baseSchemaNode = ctxNode.getDataSchemaNode();
140 if (result instanceof ContainerNode) {
141 final var builder = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
142 buildCont(builder, (ContainerNode) result, ctxNode, trim);
143 return builder.build();
145 final var builder = SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
146 buildMapEntryBuilder(builder, (MapEntryNode) result, ctxNode, 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 DataSchemaContextNode<?> ctxNode, final boolean trim,
155 final List<QName> keys) {
156 for (final DataContainerChild child : result.body()) {
157 final var childCtx = ctxNode.getChild(child.getIdentifier());
158 if (childCtx == null) {
159 throw new NoSuchElementException("Failed to map child " + child.getIdentifier());
162 final DataSchemaNode childSchema = childCtx.getDataSchemaNode();
163 if (child instanceof ContainerNode) {
164 final var childBuilder = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
165 buildCont(childBuilder, (ContainerNode) child, childCtx, trim);
166 builder.withChild(childBuilder.build());
167 } else if (child instanceof MapNode) {
168 final var childBuilder = SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
169 buildList(childBuilder, (MapNode) child, childCtx, trim,
170 ((ListSchemaNode) childSchema).getKeyDefinition());
171 builder.withChild(childBuilder.build());
172 } else if (child instanceof LeafNode) {
173 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
174 final Object nodeVal = child.body();
175 if (keys.contains(child.getIdentifier().getNodeType())) {
176 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
178 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
179 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
181 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
182 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
188 private static void buildList(final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder,
189 final MapNode result, DataSchemaContextNode<?> ctxNode, final boolean trim, final List<QName> keys) {
190 for (final MapEntryNode mapEntryNode : result.body()) {
191 final var childCtx = ctxNode.getChild(mapEntryNode.getIdentifier());
192 if (childCtx == null) {
193 throw new NoSuchElementException("Failed to match entry " + mapEntryNode.getIdentifier());
195 final DataSchemaNode childSchema = childCtx.getDataSchemaNode();
196 final var mapEntryBuilder = SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) childSchema);
197 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, childCtx, trim, keys);
198 builder.withChild(mapEntryBuilder.build());
202 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
203 final ContainerNode result, final DataSchemaContextNode<?> ctxNode, final boolean trim) {
204 for (final DataContainerChild child : result.body()) {
205 final var childCtx = ctxNode.getChild(child.getIdentifier());
206 if (childCtx == null) {
207 throw new NoSuchElementException("Cannot resolve child " + child.getIdentifier());
210 final DataSchemaNode childSchema = childCtx.getDataSchemaNode();
211 if (child instanceof ContainerNode) {
212 final var builderChild = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
213 buildCont(builderChild, result, childCtx, trim);
214 builder.withChild(builderChild.build());
215 } else if (child instanceof MapNode) {
216 final var childBuilder = SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
217 buildList(childBuilder, (MapNode) child, childCtx, trim,
218 ((ListSchemaNode) childSchema).getKeyDefinition());
219 builder.withChild(childBuilder.build());
220 } else if (child instanceof LeafNode) {
221 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
222 final Object nodeVal = child.body();
224 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
225 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
227 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
228 builder.withChild(ImmutableNodes.leafNode(childSchema.getQName(), child.body()));
235 * If is set specific {@link LogicalDatastoreType} in {@link RestconfStrategy}, then read this type of data from DS.
236 * If don't, we have to read all data from DS (state + config)
238 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
239 * @param closeTransactionChain If is set to true, after transaction it will close transactionChain
240 * in {@link RestconfStrategy} if any
241 * @return {@link NormalizedNode}
243 static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
244 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
245 return extractReadData(strategy, path, strategy.read(store, path));
249 * Read specific type of data {@link LogicalDatastoreType} via transaction in {@link RestconfStrategy} with
250 * specified subtrees that should only be read.
252 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
253 * @param store datastore type
254 * @param path parent path to selected fields
255 * @param closeTransactionChain if it is set to {@code true}, after transaction it will close transactionChain
256 * in {@link RestconfStrategy} if any
257 * @param fields paths to selected subtrees which should be read, relative to to the parent path
258 * @return {@link NormalizedNode}
260 private static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
261 final @NonNull LogicalDatastoreType store, final @NonNull YangInstanceIdentifier path,
262 final @NonNull List<YangInstanceIdentifier> fields) {
263 return extractReadData(strategy, path, strategy.read(store, path, fields));
266 private static NormalizedNode extractReadData(final RestconfStrategy strategy,
267 final YangInstanceIdentifier path, final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
268 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
269 FutureCallbackTx.addCallback(dataFuture, "READ", dataFactory, path);
270 return dataFactory.build();
274 * Read config and state data, then map them. Close {@link DOMTransactionChain} inside of object
275 * {@link RestconfStrategy} provided as a parameter if any.
277 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
278 * @param withDefa with-defaults parameter
279 * @param ctx schema context
280 * @return {@link NormalizedNode}
282 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
283 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
284 // PREPARE STATE DATA NODE
285 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
286 // PREPARE CONFIG DATA NODE
287 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION,
290 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
291 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
295 * Read config and state data with selected subtrees that should only be read, then map them.
296 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
298 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
299 * @param path parent path to selected fields
300 * @param withDefa with-defaults parameter
301 * @param ctx schema context
302 * @param fields paths to selected subtrees which should be read, relative to to the parent path
303 * @return {@link NormalizedNode}
305 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
306 final @NonNull YangInstanceIdentifier path, final @Nullable WithDefaultsParam withDefa,
307 final @NonNull EffectiveModelContext ctx, final @NonNull List<YangInstanceIdentifier> fields) {
308 // PREPARE STATE DATA NODE
309 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path,
312 // PREPARE CONFIG DATA NODE
313 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
315 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
316 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
319 private static NormalizedNode mergeConfigAndSTateDataIfNeeded(final NormalizedNode stateDataNode,
320 final NormalizedNode configDataNode) {
322 if (stateDataNode == null && configDataNode == null) {
326 // return config data
327 if (stateDataNode == null) {
328 return configDataNode;
332 if (configDataNode == null) {
333 return stateDataNode;
336 // merge data from config and state
337 return mergeStateAndConfigData(stateDataNode, configDataNode);
341 * Merge state and config data into a single NormalizedNode.
343 * @param stateDataNode data node of state data
344 * @param configDataNode data node of config data
345 * @return {@link NormalizedNode}
347 private static @NonNull NormalizedNode mergeStateAndConfigData(
348 final @NonNull NormalizedNode stateDataNode, final @NonNull NormalizedNode configDataNode) {
349 validateNodeMerge(stateDataNode, configDataNode);
350 if (configDataNode instanceof RpcDefinition) {
351 return prepareRpcData(configDataNode, stateDataNode);
353 return prepareData(configDataNode, stateDataNode);
358 * Validates whether the two NormalizedNodes can be merged.
360 * @param stateDataNode data node of state data
361 * @param configDataNode data node of config data
363 private static void validateNodeMerge(final @NonNull NormalizedNode stateDataNode,
364 final @NonNull NormalizedNode configDataNode) {
365 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
366 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
367 if (!moduleOfStateData.equals(moduleOfConfigData)) {
368 throw new RestconfDocumentedException("Unable to merge data from different modules.");
373 * Prepare and map data for rpc.
375 * @param configDataNode data node of config data
376 * @param stateDataNode data node of state data
377 * @return {@link NormalizedNode}
379 private static @NonNull NormalizedNode prepareRpcData(final @NonNull NormalizedNode configDataNode,
380 final @NonNull NormalizedNode stateDataNode) {
381 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
383 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
386 mapRpcDataNode(configDataNode, mapEntryBuilder);
388 mapRpcDataNode(stateDataNode, mapEntryBuilder);
390 return ImmutableNodes.mapNodeBuilder(configDataNode.getIdentifier().getNodeType())
391 .addChild(mapEntryBuilder.build())
396 * Map node to map entry builder.
398 * @param dataNode data node
399 * @param mapEntryBuilder builder for mapping data
401 private static void mapRpcDataNode(final @NonNull NormalizedNode dataNode,
402 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
403 ((ContainerNode) dataNode).body().forEach(mapEntryBuilder::addChild);
407 * Prepare and map all data from DS.
409 * @param configDataNode data node of config data
410 * @param stateDataNode data node of state data
411 * @return {@link NormalizedNode}
413 @SuppressWarnings("unchecked")
414 private static @NonNull NormalizedNode prepareData(final @NonNull NormalizedNode configDataNode,
415 final @NonNull NormalizedNode stateDataNode) {
416 if (configDataNode instanceof UserMapNode) {
417 final CollectionNodeBuilder<MapEntryNode, UserMapNode> builder = Builders
418 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
421 ((UserMapNode) configDataNode).body(), ((UserMapNode) stateDataNode).body(), builder);
423 return builder.build();
424 } else if (configDataNode instanceof MapNode) {
425 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder = ImmutableNodes
426 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
429 ((MapNode) configDataNode).body(), ((MapNode) stateDataNode).body(), builder);
431 return builder.build();
432 } else if (configDataNode instanceof MapEntryNode) {
433 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
434 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
437 ((MapEntryNode) configDataNode).body(), ((MapEntryNode) stateDataNode).body(), builder);
439 return builder.build();
440 } else if (configDataNode instanceof ContainerNode) {
441 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
442 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
445 ((ContainerNode) configDataNode).body(), ((ContainerNode) stateDataNode).body(), builder);
447 return builder.build();
448 } else if (configDataNode instanceof AugmentationNode) {
449 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
450 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
452 mapValueToBuilder(((AugmentationNode) configDataNode).body(),
453 ((AugmentationNode) stateDataNode).body(), builder);
455 return builder.build();
456 } else if (configDataNode instanceof ChoiceNode) {
457 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
458 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
461 ((ChoiceNode) configDataNode).body(), ((ChoiceNode) stateDataNode).body(), builder);
463 return builder.build();
464 } else if (configDataNode instanceof LeafNode) {
465 return ImmutableNodes.leafNode(configDataNode.getIdentifier().getNodeType(), configDataNode.body());
466 } else if (configDataNode instanceof UserLeafSetNode) {
467 final ListNodeBuilder<Object, UserLeafSetNode<Object>> builder = Builders
468 .orderedLeafSetBuilder().withNodeIdentifier(((UserLeafSetNode<?>) configDataNode).getIdentifier());
470 mapValueToBuilder(((UserLeafSetNode<Object>) configDataNode).body(),
471 ((UserLeafSetNode<Object>) stateDataNode).body(), builder);
472 return builder.build();
473 } else if (configDataNode instanceof LeafSetNode) {
474 final ListNodeBuilder<Object, SystemLeafSetNode<Object>> builder = Builders
475 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
477 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).body(),
478 ((LeafSetNode<Object>) stateDataNode).body(), builder);
479 return builder.build();
480 } else if (configDataNode instanceof LeafSetEntryNode) {
481 return Builders.leafSetEntryBuilder()
482 .withNodeIdentifier(((LeafSetEntryNode<?>) configDataNode).getIdentifier())
483 .withValue(configDataNode.body())
485 } else if (configDataNode instanceof UnkeyedListNode) {
486 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
487 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
489 mapValueToBuilder(((UnkeyedListNode) configDataNode).body(),
490 ((UnkeyedListNode) stateDataNode).body(), builder);
491 return builder.build();
492 } else if (configDataNode instanceof UnkeyedListEntryNode) {
493 final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
494 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
496 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).body(),
497 ((UnkeyedListEntryNode) stateDataNode).body(), builder);
498 return builder.build();
500 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
505 * Map value from container node to builder.
507 * @param configData collection of config data nodes
508 * @param stateData collection of state data nodes
509 * @param builder builder
511 private static <T extends NormalizedNode> void mapValueToBuilder(
512 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
513 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
514 final Map<PathArgument, T> configMap = configData.stream().collect(
515 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
516 final Map<PathArgument, T> stateMap = stateData.stream().collect(
517 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
519 // merge config and state data of children with different identifiers
520 mapDataToBuilder(configMap, stateMap, builder);
522 // merge config and state data of children with the same identifiers
523 mergeDataToBuilder(configMap, stateMap, builder);
527 * Map data with different identifiers to builder. Data with different identifiers can be just added
528 * as childs to parent node.
530 * @param configMap map of config data nodes
531 * @param stateMap map of state data nodes
532 * @param builder - builder
534 private static <T extends NormalizedNode> void mapDataToBuilder(
535 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
536 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
537 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
538 y -> builder.addChild(y.getValue()));
539 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
540 y -> builder.addChild(y.getValue()));
544 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
545 * go one level down with {@code prepareData} method.
547 * @param configMap immutable config data
548 * @param stateMap immutable state data
549 * @param builder - builder
551 @SuppressWarnings("unchecked")
552 private static <T extends NormalizedNode> void mergeDataToBuilder(
553 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
554 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
555 // it is enough to process only config data because operational contains the same data
556 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
557 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));