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.api.query.ContentParam;
23 import org.opendaylight.restconf.api.query.WithDefaultsParam;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
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.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.ChoiceNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
50 import org.opendaylight.yangtools.yang.data.util.DataSchemaContext;
51 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
53 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
55 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Util class for read data from data store via transaction.
64 * <li>all (config + state)
67 public final class ReadDataTransactionUtil {
68 private static final Logger LOG = LoggerFactory.getLogger(ReadDataTransactionUtil.class);
70 private ReadDataTransactionUtil() {
75 * Read specific type of data from data store via transaction. Close {@link DOMTransactionChain} if any
76 * inside of object {@link RestconfStrategy} provided as a parameter.
78 * @param content type of data to read (config, state, all)
79 * @param path the path to read
80 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
81 * @param withDefa value of with-defaults parameter
82 * @param ctx schema context
83 * @return {@link NormalizedNode}
85 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
86 final @NonNull YangInstanceIdentifier path,
87 final @NonNull RestconfStrategy strategy,
88 final WithDefaultsParam withDefa,
89 final EffectiveModelContext ctx) {
90 return switch (content) {
91 case ALL -> readAllData(strategy, path, withDefa, ctx);
93 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
94 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
96 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
101 * Read specific type of data from data store via transaction with specified subtrees that should only be read.
102 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
104 * @param content type of data to read (config, state, all)
105 * @param path the parent path to read
106 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
107 * @param withDefa value of with-defaults parameter
108 * @param ctx schema context
109 * @param fields paths to selected subtrees which should be read, relative to to the parent path
110 * @return {@link NormalizedNode}
112 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
113 final @NonNull YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy,
114 final @Nullable WithDefaultsParam withDefa, @NonNull final EffectiveModelContext ctx,
115 final @NonNull List<YangInstanceIdentifier> fields) {
116 return switch (content) {
117 case ALL -> readAllData(strategy, path, withDefa, ctx, fields);
119 final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, fields);
120 yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
122 case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
126 private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode readData,
127 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
128 final boolean trim = switch (withDefa) {
130 case EXPLICIT -> false;
131 case REPORT_ALL, REPORT_ALL_TAGGED -> throw new RestconfDocumentedException(
132 "Unsupported with-defaults value " + withDefa.paramValue());
135 final var ctxNode = DataSchemaContextTree.from(ctx).findChild(path).orElseThrow();
136 if (readData instanceof ContainerNode container) {
137 final var builder = Builders.containerBuilder().withNodeIdentifier(container.name());
138 buildCont(builder, container.body(), ctxNode, trim);
139 return builder.build();
140 } else if (readData instanceof MapEntryNode mapEntry) {
141 if (!(ctxNode.dataSchemaNode() instanceof ListSchemaNode listSchema)) {
142 throw new IllegalStateException("Input " + mapEntry + " does not match " + ctxNode);
145 final var builder = Builders.mapEntryBuilder().withNodeIdentifier(mapEntry.name());
146 buildMapEntryBuilder(builder, mapEntry.body(), ctxNode, trim, listSchema.getKeyDefinition());
147 return builder.build();
149 throw new IllegalStateException("Unhandled data contract " + readData.contract());
153 private static void buildMapEntryBuilder(
154 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
155 final Collection<@NonNull DataContainerChild> children, final DataSchemaContext ctxNode,
156 final boolean trim, final List<QName> keys) {
157 for (var child : children) {
158 final var childCtx = getChildContext(ctxNode, child);
160 if (child instanceof ContainerNode container) {
161 appendContainer(builder, container, childCtx, trim);
162 } else if (child instanceof MapNode map) {
163 appendMap(builder, map, childCtx, trim);
164 } else if (child instanceof LeafNode<?> leaf) {
165 appendLeaf(builder, leaf, childCtx, trim, keys);
167 // FIXME: we should never hit this, throw an ISE if this ever happens
168 LOG.debug("Ignoring unhandled child contract {}", child.contract());
173 private static void appendContainer(final DataContainerNodeBuilder<?, ?> builder, final ContainerNode container,
174 final DataSchemaContext ctxNode, final boolean trim) {
175 final var childBuilder = Builders.containerBuilder().withNodeIdentifier(container.name());
176 buildCont(childBuilder, container.body(), ctxNode, trim);
177 builder.withChild(childBuilder.build());
180 private static void appendLeaf(final DataContainerNodeBuilder<?, ?> builder, final LeafNode<?> leaf,
181 final DataSchemaContext ctxNode, final boolean trim, final List<QName> keys) {
182 if (!(ctxNode.dataSchemaNode() instanceof LeafSchemaNode leafSchema)) {
183 throw new IllegalStateException("Input " + leaf + " does not match " + ctxNode);
186 // FIXME: Document now this works with the likes of YangInstanceIdentifier. I bet it does not.
187 final var defaultVal = leafSchema.getType().getDefaultValue().orElse(null);
189 // This is a combined check for when we need to emit the leaf.
191 // We always have to emit key leaf values
192 keys.contains(leafSchema.getQName())
193 // trim == WithDefaultsParam.TRIM and the source is assumed to store explicit values:
195 // When data is retrieved with a <with-defaults> parameter equal to
196 // 'trim', data nodes MUST NOT be reported if they contain the schema
197 // default value. Non-configuration data nodes containing the schema
198 // default value MUST NOT be reported.
200 || trim && (defaultVal == null || !defaultVal.equals(leaf.body()))
201 // !trim == WithDefaultsParam.EXPLICIT and the source is assume to store explicit values... but I fail to
202 // grasp what we are doing here... emit only if it matches default ???!!!
203 // FIXME: The WithDefaultsParam.EXPLICIT says:
205 // Data nodes set to the YANG default by the client are reported.
207 // and RFC8040 (https://www.rfc-editor.org/rfc/rfc8040#page-60) says:
209 // If the "with-defaults" parameter is set to "explicit", then the
210 // server MUST adhere to the default-reporting behavior defined in
211 // Section 3.3 of [RFC6243].
213 // and then RFC6243 (https://www.rfc-editor.org/rfc/rfc6243#section-3.3) says:
215 // When data is retrieved with a <with-defaults> parameter equal to
216 // 'explicit', a data node that was set by a client to its schema
217 // default value MUST be reported. A conceptual data node that would be
218 // set by the server to the schema default value MUST NOT be reported.
219 // Non-configuration data nodes containing the schema default value MUST
222 // (rovarga): The source reports explicitly-defined leaves and does *not* create defaults by itself.
223 // This seems to disregard the 'trim = true' case semantics (see above).
224 // Combining the above, though, these checks are missing the 'non-config' check, which would
225 // distinguish, but barring that this check is superfluous and results in the wrong semantics.
226 // Without that input, this really should be covered by the previous case.
227 || !trim && defaultVal != null && defaultVal.equals(leaf.body())) {
228 builder.withChild(leaf);
232 private static void appendMap(final DataContainerNodeBuilder<?, ?> builder, final MapNode map,
233 final DataSchemaContext childCtx, final boolean trim) {
234 if (!(childCtx.dataSchemaNode() instanceof ListSchemaNode listSchema)) {
235 throw new IllegalStateException("Input " + map + " does not match " + childCtx);
238 final var childBuilder = switch (map.ordering()) {
239 case SYSTEM -> Builders.mapBuilder();
240 case USER -> Builders.orderedMapBuilder();
242 buildList(childBuilder.withNodeIdentifier(map.name()), map.body(), childCtx, trim,
243 listSchema.getKeyDefinition());
244 builder.withChild(childBuilder.build());
247 private static void buildList(final CollectionNodeBuilder<MapEntryNode, ? extends MapNode> builder,
248 final Collection<@NonNull MapEntryNode> entries, final DataSchemaContext ctxNode, final boolean trim,
249 final List<@NonNull QName> keys) {
250 for (var entry : entries) {
251 final var childCtx = getChildContext(ctxNode, entry);
252 final var mapEntryBuilder = Builders.mapEntryBuilder().withNodeIdentifier(entry.name());
253 buildMapEntryBuilder(mapEntryBuilder, entry.body(), childCtx, trim, keys);
254 builder.withChild(mapEntryBuilder.build());
258 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
259 final Collection<DataContainerChild> children, final DataSchemaContext ctxNode, final boolean trim) {
260 for (var child : children) {
261 final var childCtx = getChildContext(ctxNode, child);
262 if (child instanceof ContainerNode container) {
263 appendContainer(builder, container, childCtx, trim);
264 } else if (child instanceof MapNode map) {
265 appendMap(builder, map, childCtx, trim);
266 } else if (child instanceof LeafNode<?> leaf) {
267 appendLeaf(builder, leaf, childCtx, trim, List.of());
272 private static @NonNull DataSchemaContext getChildContext(final DataSchemaContext ctxNode,
273 final NormalizedNode child) {
274 final var childId = child.name();
275 final var childCtx = ctxNode instanceof DataSchemaContext.Composite composite ? composite.childByArg(childId)
277 if (childCtx == null) {
278 throw new NoSuchElementException("Cannot resolve child " + childId + " in " + ctxNode);
284 * If is set specific {@link LogicalDatastoreType} in {@link RestconfStrategy}, then read this type of data from DS.
285 * If don't, we have to read all data from DS (state + config)
287 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
288 * @param closeTransactionChain If is set to true, after transaction it will close transactionChain
289 * in {@link RestconfStrategy} if any
290 * @return {@link NormalizedNode}
292 static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
293 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
294 return extractReadData(path, strategy.read(store, path));
298 * Read specific type of data {@link LogicalDatastoreType} via transaction in {@link RestconfStrategy} with
299 * specified subtrees that should only be read.
301 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
302 * @param store datastore type
303 * @param path parent path to selected fields
304 * @param closeTransactionChain if it is set to {@code true}, after transaction it will close transactionChain
305 * in {@link RestconfStrategy} if any
306 * @param fields paths to selected subtrees which should be read, relative to to the parent path
307 * @return {@link NormalizedNode}
309 private static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
310 final @NonNull LogicalDatastoreType store, final @NonNull YangInstanceIdentifier path,
311 final @NonNull List<YangInstanceIdentifier> fields) {
312 return extractReadData(path, strategy.read(store, path, fields));
315 private static @Nullable NormalizedNode extractReadData(final YangInstanceIdentifier path,
316 final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
317 return TransactionUtil.syncAccess(dataFuture, path).orElse(null);
321 * Read config and state data, then map them. Close {@link DOMTransactionChain} inside of object
322 * {@link RestconfStrategy} provided as a parameter if any.
324 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
325 * @param withDefa with-defaults parameter
326 * @param ctx schema context
327 * @return {@link NormalizedNode}
329 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
330 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
331 // PREPARE STATE DATA NODE
332 final var stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
333 // PREPARE CONFIG DATA NODE
334 final var configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
336 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
337 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
341 * Read config and state data with selected subtrees that should only be read, then map them.
342 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
344 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
345 * @param path parent path to selected fields
346 * @param withDefa with-defaults parameter
347 * @param ctx schema context
348 * @param fields paths to selected subtrees which should be read, relative to to the parent path
349 * @return {@link NormalizedNode}
351 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
352 final @NonNull YangInstanceIdentifier path, final @Nullable WithDefaultsParam withDefa,
353 final @NonNull EffectiveModelContext ctx, final @NonNull List<YangInstanceIdentifier> fields) {
354 // PREPARE STATE DATA NODE
355 final var stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
356 // PREPARE CONFIG DATA NODE
357 final var configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, fields);
359 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
360 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
363 private static NormalizedNode mergeConfigAndSTateDataIfNeeded(final NormalizedNode stateDataNode,
364 final NormalizedNode configDataNode) {
366 if (stateDataNode == null && configDataNode == null) {
370 // return config data
371 if (stateDataNode == null) {
372 return configDataNode;
376 if (configDataNode == null) {
377 return stateDataNode;
380 // merge data from config and state
381 return mergeStateAndConfigData(stateDataNode, configDataNode);
385 * Merge state and config data into a single NormalizedNode.
387 * @param stateDataNode data node of state data
388 * @param configDataNode data node of config data
389 * @return {@link NormalizedNode}
391 private static @NonNull NormalizedNode mergeStateAndConfigData(
392 final @NonNull NormalizedNode stateDataNode, final @NonNull NormalizedNode configDataNode) {
393 validateNodeMerge(stateDataNode, configDataNode);
394 // FIXME: this check is bogus, as it confuses yang.data.api (NormalizedNode) with yang.model.api (RpcDefinition)
395 if (configDataNode instanceof RpcDefinition) {
396 return prepareRpcData(configDataNode, stateDataNode);
398 return prepareData(configDataNode, stateDataNode);
403 * Validates whether the two NormalizedNodes can be merged.
405 * @param stateDataNode data node of state data
406 * @param configDataNode data node of config data
408 private static void validateNodeMerge(final @NonNull NormalizedNode stateDataNode,
409 final @NonNull NormalizedNode configDataNode) {
410 final QNameModule moduleOfStateData = stateDataNode.name().getNodeType().getModule();
411 final QNameModule moduleOfConfigData = configDataNode.name().getNodeType().getModule();
412 if (!moduleOfStateData.equals(moduleOfConfigData)) {
413 throw new RestconfDocumentedException("Unable to merge data from different modules.");
418 * Prepare and map data for rpc.
420 * @param configDataNode data node of config data
421 * @param stateDataNode data node of state data
422 * @return {@link NormalizedNode}
424 private static @NonNull NormalizedNode prepareRpcData(final @NonNull NormalizedNode configDataNode,
425 final @NonNull NormalizedNode stateDataNode) {
426 final var mapEntryBuilder = Builders.mapEntryBuilder()
427 .withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.name());
430 mapRpcDataNode(configDataNode, mapEntryBuilder);
432 mapRpcDataNode(stateDataNode, mapEntryBuilder);
434 return Builders.mapBuilder()
435 .withNodeIdentifier(NodeIdentifier.create(configDataNode.name().getNodeType()))
436 .addChild(mapEntryBuilder.build())
441 * Map node to map entry builder.
443 * @param dataNode data node
444 * @param mapEntryBuilder builder for mapping data
446 private static void mapRpcDataNode(final @NonNull NormalizedNode dataNode,
447 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
448 ((ContainerNode) dataNode).body().forEach(mapEntryBuilder::addChild);
452 * Prepare and map all data from DS.
454 * @param configDataNode data node of config data
455 * @param stateDataNode data node of state data
456 * @return {@link NormalizedNode}
458 @SuppressWarnings("unchecked")
459 private static @NonNull NormalizedNode prepareData(final @NonNull NormalizedNode configDataNode,
460 final @NonNull NormalizedNode stateDataNode) {
461 if (configDataNode instanceof UserMapNode configMap) {
462 final var builder = Builders.orderedMapBuilder().withNodeIdentifier(configMap.name());
463 mapValueToBuilder(configMap.body(), ((UserMapNode) stateDataNode).body(), builder);
464 return builder.build();
465 } else if (configDataNode instanceof SystemMapNode configMap) {
466 final var builder = Builders.mapBuilder().withNodeIdentifier(configMap.name());
467 mapValueToBuilder(configMap.body(), ((SystemMapNode) stateDataNode).body(), builder);
468 return builder.build();
469 } else if (configDataNode instanceof MapEntryNode configEntry) {
470 final var builder = Builders.mapEntryBuilder().withNodeIdentifier(configEntry.name());
471 mapValueToBuilder(configEntry.body(), ((MapEntryNode) stateDataNode).body(), builder);
472 return builder.build();
473 } else if (configDataNode instanceof ContainerNode configContaienr) {
474 final var builder = Builders.containerBuilder().withNodeIdentifier(configContaienr.name());
475 mapValueToBuilder(configContaienr.body(), ((ContainerNode) stateDataNode).body(), builder);
476 return builder.build();
477 } else if (configDataNode instanceof ChoiceNode configChoice) {
478 final var builder = Builders.choiceBuilder().withNodeIdentifier(configChoice.name());
479 mapValueToBuilder(configChoice.body(), ((ChoiceNode) stateDataNode).body(), builder);
480 return builder.build();
481 } else if (configDataNode instanceof LeafNode configLeaf) {
482 // config trumps oper
484 } else if (configDataNode instanceof UserLeafSetNode) {
485 final var configLeafSet = (UserLeafSetNode<Object>) configDataNode;
486 final var builder = Builders.<Object>orderedLeafSetBuilder().withNodeIdentifier(configLeafSet.name());
487 mapValueToBuilder(configLeafSet.body(), ((UserLeafSetNode<Object>) stateDataNode).body(), builder);
488 return builder.build();
489 } else if (configDataNode instanceof SystemLeafSetNode) {
490 final var configLeafSet = (SystemLeafSetNode<Object>) configDataNode;
491 final var builder = Builders.<Object>leafSetBuilder().withNodeIdentifier(configLeafSet.name());
492 mapValueToBuilder(configLeafSet.body(), ((SystemLeafSetNode<Object>) stateDataNode).body(), builder);
493 return builder.build();
494 } else if (configDataNode instanceof LeafSetEntryNode<?> configEntry) {
495 // config trumps oper
497 } else if (configDataNode instanceof UnkeyedListNode configList) {
498 final var builder = Builders.unkeyedListBuilder().withNodeIdentifier(configList.name());
499 mapValueToBuilder(configList.body(), ((UnkeyedListNode) stateDataNode).body(), builder);
500 return builder.build();
501 } else if (configDataNode instanceof UnkeyedListEntryNode configEntry) {
502 final var builder = Builders.unkeyedListEntryBuilder().withNodeIdentifier(configEntry.name());
503 mapValueToBuilder(configEntry.body(), ((UnkeyedListEntryNode) stateDataNode).body(), builder);
504 return builder.build();
506 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
511 * Map value from container node to builder.
513 * @param configData collection of config data nodes
514 * @param stateData collection of state data nodes
515 * @param builder builder
517 private static <T extends NormalizedNode> void mapValueToBuilder(
518 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
519 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
520 final var configMap = configData.stream().collect(Collectors.toMap(NormalizedNode::name, Function.identity()));
521 final var stateMap = stateData.stream().collect(Collectors.toMap(NormalizedNode::name, Function.identity()));
523 // merge config and state data of children with different identifiers
524 mapDataToBuilder(configMap, stateMap, builder);
526 // merge config and state data of children with the same identifiers
527 mergeDataToBuilder(configMap, stateMap, builder);
531 * Map data with different identifiers to builder. Data with different identifiers can be just added
532 * as childs to parent node.
534 * @param configMap map of config data nodes
535 * @param stateMap map of state data nodes
536 * @param builder - builder
538 private static <T extends NormalizedNode> void mapDataToBuilder(
539 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
540 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
541 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
542 y -> builder.addChild(y.getValue()));
543 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
544 y -> builder.addChild(y.getValue()));
548 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
549 * go one level down with {@code prepareData} method.
551 * @param configMap immutable config data
552 * @param stateMap immutable state data
553 * @param builder - builder
555 @SuppressWarnings("unchecked")
556 private static <T extends NormalizedNode> void mergeDataToBuilder(
557 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
558 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
559 // it is enough to process only config data because operational contains the same data
560 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
561 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));