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.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.common.context.WriterParameters;
31 import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder;
32 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
33 import org.opendaylight.restconf.common.errors.RestconfError;
34 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
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.LeafSchemaNode;
72 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
74 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
75 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
78 * Util class for read data from data store via transaction.
82 * <li>all (config + state)
86 public final class ReadDataTransactionUtil {
88 private ReadDataTransactionUtil() {
89 throw new UnsupportedOperationException("Util class.");
93 * Parse parameters from URI request and check their types and values.
97 * {@link InstanceIdentifierContext}
101 * set tagged for {@link WriterParameters}
102 * @return {@link WriterParameters}
104 public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
105 final @Nullable UriInfo uriInfo, final boolean tagged) {
106 return parseParams(identifier, uriInfo, tagged);
110 * Parse parameters from URI request and check their types and values.
114 * {@link InstanceIdentifierContext}
117 * @return {@link WriterParameters}
119 public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
120 final @Nullable UriInfo uriInfo) {
121 return parseParams(identifier, uriInfo, false);
124 private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
125 final boolean tagged) {
126 final WriterParametersBuilder builder = new WriterParametersBuilder();
127 builder.setTagged(tagged);
129 if (uriInfo == null) {
130 return builder.build();
133 // check only allowed parameters
134 ParametersUtil.checkParametersTypes(
135 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
136 uriInfo.getQueryParameters().keySet(),
137 RestconfDataServiceConstant.ReadData.CONTENT,
138 RestconfDataServiceConstant.ReadData.DEPTH,
139 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
141 // read parameters from URI or set default values
142 final List<String> content = uriInfo.getQueryParameters().getOrDefault(
143 RestconfDataServiceConstant.ReadData.CONTENT,
144 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
145 final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
146 RestconfDataServiceConstant.ReadData.DEPTH,
147 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
149 final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
150 RestconfDataServiceConstant.ReadData.FIELDS,
151 Collections.emptyList());
153 // parameter can be in URI at most once
154 ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
155 ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
156 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
158 // check and set content
159 final String contentValue = content.get(0);
160 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
161 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
162 && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
163 throw new RestconfDocumentedException(
164 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
165 "Invalid content parameter: " + contentValue, null,
166 "The content parameter value must be either config, nonconfig or all (default)"));
170 builder.setContent(content.get(0));
172 // check and set depth
173 if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
174 final Integer value = Ints.tryParse(depth.get(0));
177 || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
178 && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
179 throw new RestconfDocumentedException(
180 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
181 "Invalid depth parameter: " + depth, null,
182 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
184 builder.setDepth(value);
188 // check and set fields
189 if (!fields.isEmpty()) {
190 builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
193 return builder.build();
197 * Read specific type of data from data store via transaction.
199 * @param valueOfContent
200 * type of data to read (config, state, all)
201 * @param transactionNode
202 * {@link TransactionVarsWrapper} - wrapper for variables
203 * @param schemaContext
205 * @return {@link NormalizedNode}
207 public static @Nullable NormalizedNode<?, ?> readData(final @NonNull String valueOfContent,
208 final @NonNull TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
209 return readData(valueOfContent, transactionNode, null, schemaContext);
213 * Read specific type of data from data store via transaction.
215 * @param valueOfContent
216 * type of data to read (config, state, all)
217 * @param transactionNode
218 * {@link TransactionVarsWrapper} - wrapper for variables
220 * vaule of with-defaults parameter
223 * @return {@link NormalizedNode}
225 public static @Nullable NormalizedNode<?, ?> readData(final @NonNull String valueOfContent,
226 final @NonNull TransactionVarsWrapper transactionNode, final String withDefa, final SchemaContext ctx) {
227 switch (valueOfContent) {
228 case RestconfDataServiceConstant.ReadData.CONFIG:
229 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
230 if (withDefa == null) {
231 return readDataViaTransaction(transactionNode);
233 return prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
234 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
236 case RestconfDataServiceConstant.ReadData.NONCONFIG:
237 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
238 return readDataViaTransaction(transactionNode);
240 case RestconfDataServiceConstant.ReadData.ALL:
241 return readAllData(transactionNode, withDefa, ctx);
244 throw new RestconfDocumentedException(
245 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
246 "Invalid content parameter: " + valueOfContent, null,
247 "The content parameter value must be either config, nonconfig or all (default)"));
252 * Read specific type of data from data store via transaction and if identifier read data from
253 * streams then put streams from actual schema context to datastore.
256 * identifier of data to read
258 * type of data to read (config, state, all)
259 * @param transactionNode
260 * {@link TransactionVarsWrapper} - wrapper for variables
262 * vaule of with-defaults parameter
263 * @param schemaContextRef
267 * @return {@link NormalizedNode}
269 public static NormalizedNode<?, ?> readData(final String identifier, final String content,
270 final TransactionVarsWrapper transactionNode, final String withDefa,
271 final SchemaContextRef schemaContextRef, final UriInfo uriInfo) {
272 final SchemaContext schemaContext = schemaContextRef.get();
273 if (identifier != null && identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
274 createAllYangNotificationStreams(transactionNode, schemaContextRef, uriInfo);
276 return readData(content, transactionNode, withDefa, schemaContext);
279 private static void createAllYangNotificationStreams(final TransactionVarsWrapper transactionNode,
280 final SchemaContextRef schemaContextRef, final UriInfo uriInfo) {
281 final DOMDataTreeReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
282 final boolean exist = SubscribeToStreamUtil.checkExist(schemaContextRef.get(), wTx);
284 for (final NotificationDefinition notificationDefinition : schemaContextRef.get().getNotifications()) {
285 final NotificationListenerAdapter notifiStreamXML =
286 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
287 NotificationOutputType.XML);
288 final NotificationListenerAdapter notifiStreamJSON =
289 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
290 NotificationOutputType.JSON);
291 writeNotificationStreamToDatastore(schemaContextRef, uriInfo, wTx, exist, notifiStreamXML);
292 writeNotificationStreamToDatastore(schemaContextRef, uriInfo, wTx, exist, notifiStreamJSON);
294 SubscribeToStreamUtil.submitData(wTx);
297 private static void writeNotificationStreamToDatastore(final SchemaContextRef schemaContextRef,
298 final UriInfo uriInfo, final DOMDataTreeReadWriteTransaction readWriteTransaction, final boolean exist,
299 final NotificationListenerAdapter listener) {
300 final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
301 final NormalizedNode mapToStreams = RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
302 listener.getSchemaPath().getLastComponent(), schemaContextRef.get().getNotifications(), null,
303 listener.getOutputType(), uri,
304 SubscribeToStreamUtil.getMonitoringModule(schemaContextRef.get()), exist);
305 SubscribeToStreamUtil.writeDataToDS(schemaContextRef.get(),
306 listener.getSchemaPath().getLastComponent().getLocalName(), readWriteTransaction, exist, mapToStreams);
309 private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
310 final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
320 throw new RestconfDocumentedException("");
323 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
324 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
325 if (result instanceof ContainerNode) {
326 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder =
327 Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
328 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
329 return builder.build();
331 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
332 Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
333 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
334 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
335 return builder.build();
339 private static void buildMapEntryBuilder(
340 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
341 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
342 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
343 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
344 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
345 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
346 if (child instanceof ContainerNode) {
347 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> childBuilder =
348 Builders.containerBuilder((ContainerSchemaNode) childSchema);
349 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
350 builder.withChild(childBuilder.build());
351 } else if (child instanceof MapNode) {
352 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
353 Builders.mapBuilder((ListSchemaNode) childSchema);
354 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
355 ((ListSchemaNode) childSchema).getKeyDefinition());
356 builder.withChild(childBuilder.build());
357 } else if (child instanceof LeafNode) {
358 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
359 final Object nodeVal = child.getValue();
360 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
361 Builders.leafBuilder((LeafSchemaNode) childSchema);
362 if (keys.contains(child.getNodeType())) {
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());
372 if (defaultVal != null && defaultVal.equals(nodeVal)) {
373 leafBuilder.withValue(((LeafNode) child).getValue());
374 builder.withChild(leafBuilder.build());
382 private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
383 final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
384 final List<QName> keys) {
385 for (final MapEntryNode mapEntryNode : result.getValue()) {
386 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
387 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
388 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
389 Builders.mapEntryBuilder((ListSchemaNode) childSchema);
390 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
391 builder.withChild(mapEntryBuilder.build());
395 private static void buildCont(final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder,
396 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
397 final YangInstanceIdentifier actualPath, final boolean trim) {
398 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
399 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
400 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
401 if (child instanceof ContainerNode) {
402 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builderChild =
403 Builders.containerBuilder((ContainerSchemaNode) childSchema);
404 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
405 builder.withChild(builderChild.build());
406 } else if (child instanceof MapNode) {
407 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
408 Builders.mapBuilder((ListSchemaNode) childSchema);
409 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
410 ((ListSchemaNode) childSchema).getKeyDefinition());
411 builder.withChild(childBuilder.build());
412 } else if (child instanceof LeafNode) {
413 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
414 final Object nodeVal = child.getValue();
415 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
416 Builders.leafBuilder((LeafSchemaNode) childSchema);
418 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
419 leafBuilder.withValue(((LeafNode) child).getValue());
420 builder.withChild(leafBuilder.build());
423 if (defaultVal != null && defaultVal.equals(nodeVal)) {
424 leafBuilder.withValue(((LeafNode) child).getValue());
425 builder.withChild(leafBuilder.build());
433 * If is set specific {@link LogicalDatastoreType} in
434 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
435 * don't, we have to read all data from DS (state + config)
437 * @param transactionNode
438 * {@link TransactionVarsWrapper} - wrapper for variables
439 * @return {@link NormalizedNode}
441 private static @Nullable NormalizedNode<?, ?> readDataViaTransaction(
442 final @NonNull TransactionVarsWrapper transactionNode) {
443 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
444 try (DOMDataTreeReadTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
445 final FluentFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = tx.read(
446 transactionNode.getLogicalDatastoreType(),
447 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
448 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
451 return dataFactory.build();
455 * Read config and state data, then map them.
457 * @param transactionNode
458 * {@link TransactionVarsWrapper} - wrapper for variables
460 * with-defaults parameter
463 * @return {@link NormalizedNode}
465 private static @Nullable NormalizedNode<?, ?> readAllData(final @NonNull TransactionVarsWrapper transactionNode,
466 final String withDefa, final SchemaContext ctx) {
467 // PREPARE STATE DATA NODE
468 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
469 final NormalizedNode<?, ?> stateDataNode = readDataViaTransaction(transactionNode);
471 // PREPARE CONFIG DATA NODE
472 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
473 final NormalizedNode<?, ?> configDataNode;
474 if (withDefa == null) {
475 configDataNode = readDataViaTransaction(transactionNode);
477 configDataNode = prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
478 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
482 if (stateDataNode == null && configDataNode == null) {
486 // return config data
487 if (stateDataNode == null) {
488 return configDataNode;
492 if (configDataNode == null) {
493 return stateDataNode;
496 // merge data from config and state
497 return mergeStateAndConfigData(stateDataNode, configDataNode);
501 * Merge state and config data into a single NormalizedNode.
503 * @param stateDataNode
504 * data node of state data
505 * @param configDataNode
506 * data node of config data
507 * @return {@link NormalizedNode}
509 private static @NonNull NormalizedNode<?, ?> mergeStateAndConfigData(
510 final @NonNull NormalizedNode<?, ?> stateDataNode, final @NonNull NormalizedNode<?, ?> configDataNode) {
511 validateNodeMerge(stateDataNode, configDataNode);
512 if (configDataNode instanceof RpcDefinition) {
513 return prepareRpcData(configDataNode, stateDataNode);
515 return prepareData(configDataNode, stateDataNode);
520 * Validates whether the two NormalizedNodes can be merged.
522 * @param stateDataNode
523 * data node of state data
524 * @param configDataNode
525 * data node of config data
527 private static void validateNodeMerge(final @NonNull NormalizedNode<?, ?> stateDataNode,
528 final @NonNull NormalizedNode<?, ?> configDataNode) {
529 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
530 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
531 if (!moduleOfStateData.equals(moduleOfConfigData)) {
532 throw new RestconfDocumentedException("Unable to merge data from different modules.");
537 * Prepare and map data for rpc.
539 * @param configDataNode
540 * data node of config data
541 * @param stateDataNode
542 * data node of state data
543 * @return {@link NormalizedNode}
545 private static @NonNull NormalizedNode<?, ?> prepareRpcData(final @NonNull NormalizedNode<?, ?> configDataNode,
546 final @NonNull NormalizedNode<?, ?> stateDataNode) {
547 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
549 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
552 mapRpcDataNode(configDataNode, mapEntryBuilder);
554 mapRpcDataNode(stateDataNode, mapEntryBuilder);
556 return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
560 * Map node to map entry builder.
564 * @param mapEntryBuilder
565 * builder for mapping data
567 private static void mapRpcDataNode(final @NonNull NormalizedNode<?, ?> dataNode,
568 final @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
569 ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
573 * Prepare and map all data from DS.
575 * @param configDataNode
576 * data node of config data
577 * @param stateDataNode
578 * data node of state data
579 * @return {@link NormalizedNode}
581 @SuppressWarnings("unchecked")
582 private static @NonNull NormalizedNode<?, ?> prepareData(final @NonNull NormalizedNode<?, ?> configDataNode,
583 final @NonNull NormalizedNode<?, ?> stateDataNode) {
584 if (configDataNode instanceof OrderedMapNode) {
585 final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = Builders
586 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
589 ((OrderedMapNode) configDataNode).getValue(), ((OrderedMapNode) stateDataNode).getValue(), builder);
591 return builder.build();
592 } else if (configDataNode instanceof MapNode) {
593 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
594 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
597 ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
599 return builder.build();
600 } else if (configDataNode instanceof MapEntryNode) {
601 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
602 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
605 ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
607 return builder.build();
608 } else if (configDataNode instanceof ContainerNode) {
609 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
610 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
613 ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
615 return builder.build();
616 } else if (configDataNode instanceof AugmentationNode) {
617 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
618 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
620 mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
621 ((AugmentationNode) stateDataNode).getValue(), builder);
623 return builder.build();
624 } else if (configDataNode instanceof ChoiceNode) {
625 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
626 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
629 ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
631 return builder.build();
632 } else if (configDataNode instanceof LeafNode) {
633 return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
634 } else if (configDataNode instanceof OrderedLeafSetNode) {
635 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
636 .orderedLeafSetBuilder().withNodeIdentifier(((OrderedLeafSetNode<?>) configDataNode).getIdentifier());
638 mapValueToBuilder(((OrderedLeafSetNode<Object>) configDataNode).getValue(),
639 ((OrderedLeafSetNode<Object>) stateDataNode).getValue(), builder);
640 return builder.build();
641 } else if (configDataNode instanceof LeafSetNode) {
642 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
643 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
645 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).getValue(),
646 ((LeafSetNode<Object>) stateDataNode).getValue(), builder);
647 return builder.build();
648 } else if (configDataNode instanceof UnkeyedListNode) {
649 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
650 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
652 mapValueToBuilder(((UnkeyedListNode) configDataNode).getValue(),
653 ((UnkeyedListNode) stateDataNode).getValue(), builder);
654 return builder.build();
655 } else if (configDataNode instanceof UnkeyedListEntryNode) {
656 final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
657 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
659 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).getValue(),
660 ((UnkeyedListEntryNode) stateDataNode).getValue(), builder);
661 return builder.build();
663 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
668 * Map value from container node to builder.
671 * collection of config data nodes
673 * collection of state data nodes
677 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
678 final @NonNull Collection<T> configData, final @NonNull Collection<T> stateData,
679 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
680 final Map<PathArgument, T> configMap = configData.stream().collect(
681 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
682 final Map<PathArgument, T> stateMap = stateData.stream().collect(
683 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
685 // merge config and state data of children with different identifiers
686 mapDataToBuilder(configMap, stateMap, builder);
688 // merge config and state data of children with the same identifiers
689 mergeDataToBuilder(configMap, stateMap, builder);
693 * Map data with different identifiers to builder. Data with different identifiers can be just added
694 * as childs to parent node.
697 * map of config data nodes
699 * map of state data nodes
703 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
704 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
705 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
706 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
707 y -> builder.addChild(y.getValue()));
708 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
709 y -> builder.addChild(y.getValue()));
713 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
714 * go one level down with {@code prepareData} method.
717 * immutable config data
719 * immutable state data
723 @SuppressWarnings("unchecked")
724 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
725 final @NonNull Map<PathArgument, T> configMap, final @NonNull Map<PathArgument, T> stateMap,
726 final @NonNull NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
727 // it is enough to process only config data because operational contains the same data
728 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
729 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));