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 static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAMS_PATH;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH_PART;
13 import com.google.common.primitives.Ints;
14 import com.google.common.util.concurrent.FluentFuture;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
20 import java.util.Optional;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import javax.ws.rs.core.UriInfo;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
30 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
31 import org.opendaylight.restconf.common.context.WriterParameters;
32 import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.restconf.common.errors.RestconfError;
35 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
36 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
37 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
38 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter;
39 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.QNameModule;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
51 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
61 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
64 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
65 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
66 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
67 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
68 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
69 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
71 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
72 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
75 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
76 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
79 * Util class for read data from data store via transaction.
83 * <li>all (config + state)
87 public final class ReadDataTransactionUtil {
89 private ReadDataTransactionUtil() {
90 throw new UnsupportedOperationException("Util class.");
94 * Parse parameters from URI request and check their types and values.
97 * {@link InstanceIdentifierContext}
100 * @return {@link WriterParameters}
102 public static WriterParameters parseUriParameters(final InstanceIdentifierContext<?> identifier,
103 final UriInfo uriInfo) {
104 final WriterParametersBuilder builder = new WriterParametersBuilder();
106 if (uriInfo == null) {
107 return builder.build();
110 // check only allowed parameters
111 ParametersUtil.checkParametersTypes(
112 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
113 uriInfo.getQueryParameters().keySet(),
114 RestconfDataServiceConstant.ReadData.CONTENT,
115 RestconfDataServiceConstant.ReadData.DEPTH,
116 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
118 // read parameters from URI or set default values
119 final List<String> content = uriInfo.getQueryParameters().getOrDefault(
120 RestconfDataServiceConstant.ReadData.CONTENT,
121 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
122 final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
123 RestconfDataServiceConstant.ReadData.DEPTH,
124 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
125 final List<String> withDefaults = uriInfo.getQueryParameters().getOrDefault(
126 RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
127 Collections.emptyList());
129 final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
130 RestconfDataServiceConstant.ReadData.FIELDS,
131 Collections.emptyList());
133 // parameter can be in URI at most once
134 ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
135 ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
136 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
137 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
139 // check and set content
140 final String contentValue = content.get(0);
141 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
142 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
143 && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
144 throw new RestconfDocumentedException(
145 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
146 "Invalid content parameter: " + contentValue, null,
147 "The content parameter value must be either config, nonconfig or all (default)"));
151 builder.setContent(content.get(0));
153 // check and set depth
154 if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
155 final Integer value = Ints.tryParse(depth.get(0));
158 || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
159 && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
160 throw new RestconfDocumentedException(
161 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
162 "Invalid depth parameter: " + depth, null,
163 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
165 builder.setDepth(value);
169 // check and set fields
170 if (!fields.isEmpty()) {
171 builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
174 // check and set withDefaults parameter
175 if (!withDefaults.isEmpty()) {
176 switch (withDefaults.get(0)) {
177 case RestconfDataServiceConstant.ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE:
178 builder.setTagged(true);
180 case RestconfDataServiceConstant.ReadData.REPORT_ALL_DEFAULT_VALUE:
183 builder.setWithDefault(withDefaults.get(0));
187 return builder.build();
191 * Read specific type of data from data store via transaction.
193 * @param valueOfContent
194 * type of data to read (config, state, all)
195 * @param transactionNode
196 * {@link TransactionVarsWrapper} - wrapper for variables
197 * @param schemaContext
199 * @return {@link NormalizedNode}
201 public static @Nullable NormalizedNode<?, ?> readData(final @NonNull String valueOfContent,
202 final @NonNull TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
203 return readData(valueOfContent, transactionNode, null, schemaContext);
207 * Read specific type of data from data store via transaction. Close {@link DOMTransactionChain} inside of object
208 * {@link TransactionVarsWrapper} provided as a parameter.
210 * @param valueOfContent
211 * type of data to read (config, state, all)
212 * @param transactionNode
213 * {@link TransactionVarsWrapper} - wrapper for variables
215 * value of with-defaults parameter
218 * @return {@link NormalizedNode}
220 public static @Nullable NormalizedNode<?, ?> readData(final @NonNull String valueOfContent,
221 final @NonNull TransactionVarsWrapper transactionNode, final String withDefa, final SchemaContext ctx) {
222 switch (valueOfContent) {
223 case RestconfDataServiceConstant.ReadData.CONFIG:
224 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
225 if (withDefa == null) {
226 return readDataViaTransaction(transactionNode);
228 return prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
229 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
231 case RestconfDataServiceConstant.ReadData.NONCONFIG:
232 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
233 return readDataViaTransaction(transactionNode);
235 case RestconfDataServiceConstant.ReadData.ALL:
236 return readAllData(transactionNode, withDefa, ctx);
239 transactionNode.getTransactionChain().close();
240 throw new RestconfDocumentedException(
241 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
242 "Invalid content parameter: " + valueOfContent, null,
243 "The content parameter value must be either config, nonconfig or all (default)"));
248 * Read specific type of data from data store via transaction and if identifier read data from
249 * streams then put streams from actual schema context to datastore.
252 * identifier of data to read
254 * type of data to read (config, state, all)
255 * @param transactionNode
256 * {@link TransactionVarsWrapper} - wrapper for variables
258 * vaule of with-defaults parameter
259 * @param schemaContext
263 * @return {@link NormalizedNode}
265 public static NormalizedNode<?, ?> readData(final String identifier, final String content,
266 final TransactionVarsWrapper transactionNode, final String withDefa,
267 final EffectiveModelContext schemaContext, final UriInfo uriInfo) {
268 if (identifier != null && identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
269 createAllYangNotificationStreams(transactionNode, schemaContext, uriInfo);
271 return readData(content, transactionNode, withDefa, schemaContext);
274 private static void createAllYangNotificationStreams(final TransactionVarsWrapper transactionNode,
275 final EffectiveModelContext schemaContext, final UriInfo uriInfo) {
276 final DOMDataTreeReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
277 final boolean exist = SubscribeToStreamUtil.checkExist(schemaContext, wTx);
279 for (final NotificationDefinition notificationDefinition : schemaContext.getNotifications()) {
280 final NotificationListenerAdapter notifiStreamXML =
281 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext,
282 NotificationOutputType.XML);
283 final NotificationListenerAdapter notifiStreamJSON =
284 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext,
285 NotificationOutputType.JSON);
286 writeNotificationStreamToDatastore(schemaContext, uriInfo, wTx, exist, notifiStreamXML);
287 writeNotificationStreamToDatastore(schemaContext, uriInfo, wTx, exist, notifiStreamJSON);
289 SubscribeToStreamUtil.submitData(wTx);
292 private static void writeNotificationStreamToDatastore(final EffectiveModelContext schemaContext,
293 final UriInfo uriInfo, final DOMDataTreeReadWriteTransaction readWriteTransaction, final boolean exist,
294 final NotificationListenerAdapter listener) {
295 final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
296 final NormalizedNode<?, ?> mapToStreams =
297 RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
298 listener.getSchemaPath().getLastComponent(), schemaContext.getNotifications(), null,
299 listener.getOutputType(), uri, SubscribeToStreamUtil.getMonitoringModule(schemaContext), exist);
300 SubscribeToStreamUtil.writeDataToDS(schemaContext,
301 listener.getSchemaPath().getLastComponent().getLocalName(), readWriteTransaction, exist, mapToStreams);
304 private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
305 final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
315 throw new RestconfDocumentedException("");
318 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
319 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
320 if (result instanceof ContainerNode) {
321 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder =
322 Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
323 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
324 return builder.build();
326 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
327 Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
328 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
329 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
330 return builder.build();
334 private static void buildMapEntryBuilder(
335 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
336 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
337 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
338 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
339 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
340 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
341 if (child instanceof ContainerNode) {
342 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> childBuilder =
343 Builders.containerBuilder((ContainerSchemaNode) childSchema);
344 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
345 builder.withChild(childBuilder.build());
346 } else if (child instanceof MapNode) {
347 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
348 Builders.mapBuilder((ListSchemaNode) childSchema);
349 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
350 ((ListSchemaNode) childSchema).getKeyDefinition());
351 builder.withChild(childBuilder.build());
352 } else if (child instanceof LeafNode) {
353 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
354 final Object nodeVal = child.getValue();
355 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
356 Builders.leafBuilder((LeafSchemaNode) childSchema);
357 if (keys.contains(child.getNodeType())) {
358 leafBuilder.withValue(((LeafNode<?>) child).getValue());
359 builder.withChild(leafBuilder.build());
362 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
363 leafBuilder.withValue(((LeafNode<?>) child).getValue());
364 builder.withChild(leafBuilder.build());
367 if (defaultVal != null && defaultVal.equals(nodeVal)) {
368 leafBuilder.withValue(((LeafNode<?>) child).getValue());
369 builder.withChild(leafBuilder.build());
377 private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
378 final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
379 final List<QName> keys) {
380 for (final MapEntryNode mapEntryNode : result.getValue()) {
381 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
382 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
383 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
384 Builders.mapEntryBuilder((ListSchemaNode) childSchema);
385 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
386 builder.withChild(mapEntryBuilder.build());
390 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
391 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
392 final YangInstanceIdentifier actualPath, final boolean trim) {
393 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
394 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
395 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
396 if (child instanceof ContainerNode) {
397 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builderChild =
398 Builders.containerBuilder((ContainerSchemaNode) childSchema);
399 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
400 builder.withChild(builderChild.build());
401 } else if (child instanceof MapNode) {
402 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
403 Builders.mapBuilder((ListSchemaNode) childSchema);
404 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
405 ((ListSchemaNode) childSchema).getKeyDefinition());
406 builder.withChild(childBuilder.build());
407 } else if (child instanceof LeafNode) {
408 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
409 final Object nodeVal = child.getValue();
410 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
411 Builders.leafBuilder((LeafSchemaNode) childSchema);
413 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
414 leafBuilder.withValue(((LeafNode<?>) child).getValue());
415 builder.withChild(leafBuilder.build());
418 if (defaultVal != null && defaultVal.equals(nodeVal)) {
419 leafBuilder.withValue(((LeafNode<?>) child).getValue());
420 builder.withChild(leafBuilder.build());
428 * If is set specific {@link LogicalDatastoreType} in
429 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
430 * don't, we have to read all data from DS (state + config).
431 * This method will close {@link org.opendaylight.mdsal.dom.api.DOMTransactionChain} inside of
432 * {@link TransactionVarsWrapper}.
434 * @param transactionNode
435 * {@link TransactionVarsWrapper} - wrapper for variables
436 * @return {@link NormalizedNode}
438 private static @Nullable NormalizedNode<?, ?> readDataViaTransaction(
439 final @NonNull TransactionVarsWrapper transactionNode) {
440 return readDataViaTransaction(transactionNode, true);
445 * If is set specific {@link LogicalDatastoreType} in
446 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
447 * don't, we have to read all data from DS (state + config)
449 * @param transactionNode
450 * {@link TransactionVarsWrapper} - wrapper for variables
451 * @param closeTransactionChain
452 * If is set to true, after transaction it will close transactionChain in {@link TransactionVarsWrapper}
453 * @return {@link NormalizedNode}
455 private static @Nullable NormalizedNode<?, ?> readDataViaTransaction(
456 final @NonNull TransactionVarsWrapper transactionNode, final boolean closeTransactionChain) {
457 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
458 try (DOMDataTreeReadTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
459 final FluentFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = tx.read(
460 transactionNode.getLogicalDatastoreType(),
461 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
462 if (closeTransactionChain) {
463 //Method close transactionChain inside of TransactionVarsWrapper, if is provide as a parameter.
464 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
465 dataFactory, transactionNode.getTransactionChain());
467 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
471 return dataFactory.build();
475 * Read config and state data, then map them. Close {@link DOMTransactionChain} inside of object
476 * {@link TransactionVarsWrapper} provided as a parameter.
478 * @param transactionNode
479 * {@link TransactionVarsWrapper} - wrapper for variables
481 * with-defaults parameter
484 * @return {@link NormalizedNode}
486 private static @Nullable NormalizedNode<?, ?> readAllData(final @NonNull TransactionVarsWrapper transactionNode,
487 final String withDefa, final SchemaContext ctx) {
488 // PREPARE STATE DATA NODE
489 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
490 final NormalizedNode<?, ?> stateDataNode = readDataViaTransaction(transactionNode, false);
492 // PREPARE CONFIG DATA NODE
493 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
494 final NormalizedNode<?, ?> configDataNode;
495 //Here will be closed transactionChain
496 if (withDefa == null) {
497 configDataNode = readDataViaTransaction(transactionNode);
499 configDataNode = prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
500 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
504 if (stateDataNode == null && configDataNode == null) {
508 // return config data
509 if (stateDataNode == null) {
510 return configDataNode;
514 if (configDataNode == null) {
515 return stateDataNode;
518 // merge data from config and state
519 return mergeStateAndConfigData(stateDataNode, configDataNode);
523 * Merge state and config data into a single NormalizedNode.
525 * @param stateDataNode
526 * data node of state data
527 * @param configDataNode
528 * data node of config data
529 * @return {@link NormalizedNode}
531 private static @NonNull NormalizedNode<?, ?> mergeStateAndConfigData(
532 final @NonNull NormalizedNode<?, ?> stateDataNode, final @NonNull NormalizedNode<?, ?> configDataNode) {
533 validateNodeMerge(stateDataNode, configDataNode);
534 if (configDataNode instanceof RpcDefinition) {
535 return prepareRpcData(configDataNode, stateDataNode);
537 return prepareData(configDataNode, stateDataNode);
542 * Validates whether the two NormalizedNodes can be merged.
544 * @param stateDataNode
545 * data node of state data
546 * @param configDataNode
547 * data node of config data
549 private static void validateNodeMerge(final @NonNull NormalizedNode<?, ?> stateDataNode,
550 final @NonNull NormalizedNode<?, ?> configDataNode) {
551 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
552 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
553 if (!moduleOfStateData.equals(moduleOfConfigData)) {
554 throw new RestconfDocumentedException("Unable to merge data from different modules.");
559 * Prepare and map data for rpc.
561 * @param configDataNode
562 * data node of config data
563 * @param stateDataNode
564 * data node of state data
565 * @return {@link NormalizedNode}
567 private static @NonNull NormalizedNode<?, ?> prepareRpcData(final @NonNull NormalizedNode<?, ?> configDataNode,
568 final @NonNull NormalizedNode<?, ?> stateDataNode) {
569 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
571 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
574 mapRpcDataNode(configDataNode, mapEntryBuilder);
576 mapRpcDataNode(stateDataNode, mapEntryBuilder);
578 return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
582 * Map node to map entry builder.
586 * @param mapEntryBuilder
587 * builder for mapping data
589 private static void mapRpcDataNode(final @NonNull NormalizedNode<?, ?> dataNode,
590 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
591 ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
595 * Prepare and map all data from DS.
597 * @param configDataNode
598 * data node of config data
599 * @param stateDataNode
600 * data node of state data
601 * @return {@link NormalizedNode}
603 @SuppressWarnings("unchecked")
604 private static @NonNull NormalizedNode<?, ?> prepareData(final @NonNull NormalizedNode<?, ?> configDataNode,
605 final @NonNull NormalizedNode<?, ?> stateDataNode) {
606 if (configDataNode instanceof OrderedMapNode) {
607 final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = Builders
608 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
611 ((OrderedMapNode) configDataNode).getValue(), ((OrderedMapNode) stateDataNode).getValue(), builder);
613 return builder.build();
614 } else if (configDataNode instanceof MapNode) {
615 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
616 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
619 ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
621 return builder.build();
622 } else if (configDataNode instanceof MapEntryNode) {
623 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
624 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
627 ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
629 return builder.build();
630 } else if (configDataNode instanceof ContainerNode) {
631 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
632 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
635 ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
637 return builder.build();
638 } else if (configDataNode instanceof AugmentationNode) {
639 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
640 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
642 mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
643 ((AugmentationNode) stateDataNode).getValue(), builder);
645 return builder.build();
646 } else if (configDataNode instanceof ChoiceNode) {
647 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
648 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
651 ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
653 return builder.build();
654 } else if (configDataNode instanceof LeafNode) {
655 return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
656 } else if (configDataNode instanceof OrderedLeafSetNode) {
657 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
658 .orderedLeafSetBuilder().withNodeIdentifier(((OrderedLeafSetNode<?>) configDataNode).getIdentifier());
660 mapValueToBuilder(((OrderedLeafSetNode<Object>) configDataNode).getValue(),
661 ((OrderedLeafSetNode<Object>) stateDataNode).getValue(), builder);
662 return builder.build();
663 } else if (configDataNode instanceof LeafSetNode) {
664 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
665 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
667 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).getValue(),
668 ((LeafSetNode<Object>) stateDataNode).getValue(), builder);
669 return builder.build();
670 } else if (configDataNode instanceof LeafSetEntryNode) {
671 return Builders.leafSetEntryBuilder()
672 .withNodeIdentifier(((LeafSetEntryNode<?>) configDataNode).getIdentifier())
673 .withValue(configDataNode.getValue())
675 } else if (configDataNode instanceof UnkeyedListNode) {
676 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
677 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
679 mapValueToBuilder(((UnkeyedListNode) configDataNode).getValue(),
680 ((UnkeyedListNode) stateDataNode).getValue(), builder);
681 return builder.build();
682 } else if (configDataNode instanceof UnkeyedListEntryNode) {
683 final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
684 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
686 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).getValue(),
687 ((UnkeyedListEntryNode) stateDataNode).getValue(), builder);
688 return builder.build();
690 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
695 * Map value from container node to builder.
698 * collection of config data nodes
700 * collection of state data nodes
704 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
705 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
706 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
707 final Map<PathArgument, T> configMap = configData.stream().collect(
708 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
709 final Map<PathArgument, T> stateMap = stateData.stream().collect(
710 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
712 // merge config and state data of children with different identifiers
713 mapDataToBuilder(configMap, stateMap, builder);
715 // merge config and state data of children with the same identifiers
716 mergeDataToBuilder(configMap, stateMap, builder);
720 * Map data with different identifiers to builder. Data with different identifiers can be just added
721 * as childs to parent node.
724 * map of config data nodes
726 * map of state data nodes
730 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
731 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
732 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
733 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
734 y -> builder.addChild(y.getValue()));
735 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
736 y -> builder.addChild(y.getValue()));
740 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
741 * go one level down with {@code prepareData} method.
744 * immutable config data
746 * immutable state data
750 @SuppressWarnings("unchecked")
751 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
752 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
753 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
754 // it is enough to process only config data because operational contains the same data
755 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
756 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));