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.common.errors.RestconfError;
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.ErrorTag;
27 import org.opendaylight.yangtools.yang.common.ErrorType;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
54 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
55 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
59 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
60 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
63 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
68 * Util class for read data from data store via transaction.
72 * <li>all (config + state)
75 public final class ReadDataTransactionUtil {
76 private ReadDataTransactionUtil() {
81 * Read specific type of data from data store via transaction. Close {@link DOMTransactionChain} if any
82 * inside of object {@link RestconfStrategy} provided as a parameter.
84 * @param content type of data to read (config, state, all)
85 * @param path the path to read
86 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
87 * @param withDefa value of with-defaults parameter
88 * @param ctx schema context
89 * @return {@link NormalizedNode}
91 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
92 final @NonNull YangInstanceIdentifier path,
93 final @NonNull RestconfStrategy strategy,
94 final WithDefaultsParam withDefa,
95 final EffectiveModelContext ctx) {
96 // FIXME: use a switch expression when they are available, removing source of RestconfDocumentedException
99 return readAllData(strategy, path, withDefa, ctx);
101 final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
102 return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
104 return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
106 throw new RestconfDocumentedException(
107 new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
108 "Invalid content parameter: " + content.paramValue(), null,
109 "The content parameter value must be either config, nonconfig or all (default)"));
114 * Read specific type of data from data store via transaction with specified subtrees that should only be read.
115 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
117 * @param content type of data to read (config, state, all)
118 * @param path the parent path to read
119 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
120 * @param withDefa value of with-defaults parameter
121 * @param ctx schema context
122 * @param fields paths to selected subtrees which should be read, relative to to the parent path
123 * @return {@link NormalizedNode}
125 public static @Nullable NormalizedNode readData(final @NonNull ContentParam content,
126 final @NonNull YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy,
127 final @Nullable WithDefaultsParam withDefa, @NonNull final EffectiveModelContext ctx,
128 final @NonNull List<YangInstanceIdentifier> fields) {
129 // FIXME: use a switch expression when they are available, removing source of RestconfDocumentedException
132 return readAllData(strategy, path, withDefa, ctx, fields);
134 final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
136 return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
138 return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
140 throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
141 "Invalid content parameter: " + content.paramValue(), null,
142 "The content parameter value must be either config, nonconfig or all (default)"));
146 private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result,
147 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
157 throw new RestconfDocumentedException("Unsupported with-defaults value " + withDefa.paramValue());
160 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
161 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
162 if (result instanceof ContainerNode) {
163 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder =
164 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
165 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
166 return builder.build();
168 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
169 SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
170 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
171 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
172 return builder.build();
176 private static void buildMapEntryBuilder(
177 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
178 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
179 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
180 for (final DataContainerChild child : result.body()) {
181 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
182 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
183 if (child instanceof ContainerNode) {
184 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> childBuilder =
185 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
186 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
187 builder.withChild(childBuilder.build());
188 } else if (child instanceof MapNode) {
189 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> childBuilder =
190 SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
191 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
192 ((ListSchemaNode) childSchema).getKeyDefinition());
193 builder.withChild(childBuilder.build());
194 } else if (child instanceof LeafNode) {
195 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
196 final Object nodeVal = child.body();
197 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
198 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) childSchema);
199 if (keys.contains(child.getIdentifier().getNodeType())) {
200 leafBuilder.withValue(((LeafNode<?>) child).body());
201 builder.withChild(leafBuilder.build());
203 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
204 leafBuilder.withValue(((LeafNode<?>) child).body());
205 builder.withChild(leafBuilder.build());
207 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
208 leafBuilder.withValue(((LeafNode<?>) child).body());
209 builder.withChild(leafBuilder.build());
215 private static void buildList(final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder,
216 final MapNode result, final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path,
217 final boolean trim, final List<QName> keys) {
218 for (final MapEntryNode mapEntryNode : result.body()) {
219 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
220 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(actualNode).orElseThrow()
221 .getDataSchemaNode();
222 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
223 SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) childSchema);
224 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
225 builder.withChild(mapEntryBuilder.build());
229 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
230 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
231 final YangInstanceIdentifier actualPath, final boolean trim) {
232 for (final DataContainerChild child : result.body()) {
233 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
234 final DataSchemaNode childSchema = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
235 if (child instanceof ContainerNode) {
236 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builderChild =
237 SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) childSchema);
238 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
239 builder.withChild(builderChild.build());
240 } else if (child instanceof MapNode) {
241 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> childBuilder =
242 SchemaAwareBuilders.mapBuilder((ListSchemaNode) childSchema);
243 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
244 ((ListSchemaNode) childSchema).getKeyDefinition());
245 builder.withChild(childBuilder.build());
246 } else if (child instanceof LeafNode) {
247 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
248 final Object nodeVal = child.body();
249 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
250 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) childSchema);
252 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
253 leafBuilder.withValue(((LeafNode<?>) child).body());
254 builder.withChild(leafBuilder.build());
256 } else if (defaultVal != null && defaultVal.equals(nodeVal)) {
257 leafBuilder.withValue(((LeafNode<?>) child).body());
258 builder.withChild(leafBuilder.build());
265 * If is set specific {@link LogicalDatastoreType} in {@link RestconfStrategy}, then read this type of data from DS.
266 * If don't, we have to read all data from DS (state + config)
268 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
269 * @param closeTransactionChain If is set to true, after transaction it will close transactionChain
270 * in {@link RestconfStrategy} if any
271 * @return {@link NormalizedNode}
273 static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
274 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
275 return extractReadData(strategy, path, strategy.read(store, path));
279 * Read specific type of data {@link LogicalDatastoreType} via transaction in {@link RestconfStrategy} with
280 * specified subtrees that should only be read.
282 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
283 * @param store datastore type
284 * @param path parent path to selected fields
285 * @param closeTransactionChain if it is set to {@code true}, after transaction it will close transactionChain
286 * in {@link RestconfStrategy} if any
287 * @param fields paths to selected subtrees which should be read, relative to to the parent path
288 * @return {@link NormalizedNode}
290 private static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
291 final @NonNull LogicalDatastoreType store, final @NonNull YangInstanceIdentifier path,
292 final @NonNull List<YangInstanceIdentifier> fields) {
293 return extractReadData(strategy, path, strategy.read(store, path, fields));
296 private static NormalizedNode extractReadData(final RestconfStrategy strategy,
297 final YangInstanceIdentifier path, final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
298 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
299 FutureCallbackTx.addCallback(dataFuture, "READ", dataFactory, path);
300 return dataFactory.build();
304 * Read config and state data, then map them. Close {@link DOMTransactionChain} inside of object
305 * {@link RestconfStrategy} provided as a parameter if any.
307 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
308 * @param withDefa with-defaults parameter
309 * @param ctx schema context
310 * @return {@link NormalizedNode}
312 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
313 final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
314 // PREPARE STATE DATA NODE
315 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
316 // PREPARE CONFIG DATA NODE
317 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION,
320 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
321 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
325 * Read config and state data with selected subtrees that should only be read, then map them.
326 * Close {@link DOMTransactionChain} inside of object {@link RestconfStrategy} provided as a parameter.
328 * @param strategy {@link RestconfStrategy} - object that perform the actual DS operations
329 * @param path parent path to selected fields
330 * @param withDefa with-defaults parameter
331 * @param ctx schema context
332 * @param fields paths to selected subtrees which should be read, relative to to the parent path
333 * @return {@link NormalizedNode}
335 private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
336 final @NonNull YangInstanceIdentifier path, final @Nullable WithDefaultsParam withDefa,
337 final @NonNull EffectiveModelContext ctx, final @NonNull List<YangInstanceIdentifier> fields) {
338 // PREPARE STATE DATA NODE
339 final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path,
342 // PREPARE CONFIG DATA NODE
343 final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
345 return mergeConfigAndSTateDataIfNeeded(stateDataNode,
346 withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
349 private static NormalizedNode mergeConfigAndSTateDataIfNeeded(final NormalizedNode stateDataNode,
350 final NormalizedNode configDataNode) {
352 if (stateDataNode == null && configDataNode == null) {
356 // return config data
357 if (stateDataNode == null) {
358 return configDataNode;
362 if (configDataNode == null) {
363 return stateDataNode;
366 // merge data from config and state
367 return mergeStateAndConfigData(stateDataNode, configDataNode);
371 * Merge state and config data into a single NormalizedNode.
373 * @param stateDataNode data node of state data
374 * @param configDataNode data node of config data
375 * @return {@link NormalizedNode}
377 private static @NonNull NormalizedNode mergeStateAndConfigData(
378 final @NonNull NormalizedNode stateDataNode, final @NonNull NormalizedNode configDataNode) {
379 validateNodeMerge(stateDataNode, configDataNode);
380 if (configDataNode instanceof RpcDefinition) {
381 return prepareRpcData(configDataNode, stateDataNode);
383 return prepareData(configDataNode, stateDataNode);
388 * Validates whether the two NormalizedNodes can be merged.
390 * @param stateDataNode data node of state data
391 * @param configDataNode data node of config data
393 private static void validateNodeMerge(final @NonNull NormalizedNode stateDataNode,
394 final @NonNull NormalizedNode configDataNode) {
395 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
396 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
397 if (!moduleOfStateData.equals(moduleOfConfigData)) {
398 throw new RestconfDocumentedException("Unable to merge data from different modules.");
403 * Prepare and map data for rpc.
405 * @param configDataNode data node of config data
406 * @param stateDataNode data node of state data
407 * @return {@link NormalizedNode}
409 private static @NonNull NormalizedNode prepareRpcData(final @NonNull NormalizedNode configDataNode,
410 final @NonNull NormalizedNode stateDataNode) {
411 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
413 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
416 mapRpcDataNode(configDataNode, mapEntryBuilder);
418 mapRpcDataNode(stateDataNode, mapEntryBuilder);
420 return ImmutableNodes.mapNodeBuilder(configDataNode.getIdentifier().getNodeType())
421 .addChild(mapEntryBuilder.build())
426 * Map node to map entry builder.
428 * @param dataNode data node
429 * @param mapEntryBuilder builder for mapping data
431 private static void mapRpcDataNode(final @NonNull NormalizedNode dataNode,
432 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
433 ((ContainerNode) dataNode).body().forEach(mapEntryBuilder::addChild);
437 * Prepare and map all data from DS.
439 * @param configDataNode data node of config data
440 * @param stateDataNode data node of state data
441 * @return {@link NormalizedNode}
443 @SuppressWarnings("unchecked")
444 private static @NonNull NormalizedNode prepareData(final @NonNull NormalizedNode configDataNode,
445 final @NonNull NormalizedNode stateDataNode) {
446 if (configDataNode instanceof UserMapNode) {
447 final CollectionNodeBuilder<MapEntryNode, UserMapNode> builder = Builders
448 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
451 ((UserMapNode) configDataNode).body(), ((UserMapNode) stateDataNode).body(), builder);
453 return builder.build();
454 } else if (configDataNode instanceof MapNode) {
455 final CollectionNodeBuilder<MapEntryNode, SystemMapNode> builder = ImmutableNodes
456 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
459 ((MapNode) configDataNode).body(), ((MapNode) stateDataNode).body(), builder);
461 return builder.build();
462 } else if (configDataNode instanceof MapEntryNode) {
463 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
464 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
467 ((MapEntryNode) configDataNode).body(), ((MapEntryNode) stateDataNode).body(), builder);
469 return builder.build();
470 } else if (configDataNode instanceof ContainerNode) {
471 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
472 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
475 ((ContainerNode) configDataNode).body(), ((ContainerNode) stateDataNode).body(), builder);
477 return builder.build();
478 } else if (configDataNode instanceof AugmentationNode) {
479 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
480 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
482 mapValueToBuilder(((AugmentationNode) configDataNode).body(),
483 ((AugmentationNode) stateDataNode).body(), builder);
485 return builder.build();
486 } else if (configDataNode instanceof ChoiceNode) {
487 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
488 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
491 ((ChoiceNode) configDataNode).body(), ((ChoiceNode) stateDataNode).body(), builder);
493 return builder.build();
494 } else if (configDataNode instanceof LeafNode) {
495 return ImmutableNodes.leafNode(configDataNode.getIdentifier().getNodeType(), configDataNode.body());
496 } else if (configDataNode instanceof UserLeafSetNode) {
497 final ListNodeBuilder<Object, UserLeafSetNode<Object>> builder = Builders
498 .orderedLeafSetBuilder().withNodeIdentifier(((UserLeafSetNode<?>) configDataNode).getIdentifier());
500 mapValueToBuilder(((UserLeafSetNode<Object>) configDataNode).body(),
501 ((UserLeafSetNode<Object>) stateDataNode).body(), builder);
502 return builder.build();
503 } else if (configDataNode instanceof LeafSetNode) {
504 final ListNodeBuilder<Object, SystemLeafSetNode<Object>> builder = Builders
505 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
507 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).body(),
508 ((LeafSetNode<Object>) stateDataNode).body(), builder);
509 return builder.build();
510 } else if (configDataNode instanceof LeafSetEntryNode) {
511 return Builders.leafSetEntryBuilder()
512 .withNodeIdentifier(((LeafSetEntryNode<?>) configDataNode).getIdentifier())
513 .withValue(configDataNode.body())
515 } else if (configDataNode instanceof UnkeyedListNode) {
516 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
517 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
519 mapValueToBuilder(((UnkeyedListNode) configDataNode).body(),
520 ((UnkeyedListNode) stateDataNode).body(), builder);
521 return builder.build();
522 } else if (configDataNode instanceof UnkeyedListEntryNode) {
523 final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
524 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
526 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).body(),
527 ((UnkeyedListEntryNode) stateDataNode).body(), builder);
528 return builder.build();
530 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
535 * Map value from container node to builder.
537 * @param configData collection of config data nodes
538 * @param stateData collection of state data nodes
539 * @param builder builder
541 private static <T extends NormalizedNode> void mapValueToBuilder(
542 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
543 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
544 final Map<PathArgument, T> configMap = configData.stream().collect(
545 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
546 final Map<PathArgument, T> stateMap = stateData.stream().collect(
547 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
549 // merge config and state data of children with different identifiers
550 mapDataToBuilder(configMap, stateMap, builder);
552 // merge config and state data of children with the same identifiers
553 mergeDataToBuilder(configMap, stateMap, builder);
557 * Map data with different identifiers to builder. Data with different identifiers can be just added
558 * as childs to parent node.
560 * @param configMap map of config data nodes
561 * @param stateMap map of state data nodes
562 * @param builder - builder
564 private static <T extends NormalizedNode> void mapDataToBuilder(
565 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
566 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
567 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
568 y -> builder.addChild(y.getValue()));
569 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
570 y -> builder.addChild(y.getValue()));
574 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
575 * go one level down with {@code prepareData} method.
577 * @param configMap immutable config data
578 * @param stateMap immutable state data
579 * @param builder - builder
581 @SuppressWarnings("unchecked")
582 private static <T extends NormalizedNode> void mergeDataToBuilder(
583 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
584 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
585 // it is enough to process only config data because operational contains the same data
586 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
587 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));