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.base.Optional;
14 import com.google.common.primitives.Ints;
15 import com.google.common.util.concurrent.CheckedFuture;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import javax.ws.rs.core.UriInfo;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
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.references.SchemaContextRef;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
37 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
38 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
39 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter;
40 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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.builder.api.CollectionNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
62 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
63 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
64 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
66 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
67 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
69 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
73 * Util class for read data from data store via transaction.
77 * <li>all (config + state)
81 public final class ReadDataTransactionUtil {
83 private ReadDataTransactionUtil() {
84 throw new UnsupportedOperationException("Util class.");
88 * Parse parameters from URI request and check their types and values.
92 * {@link InstanceIdentifierContext}
96 * set tagged for {@link WriterParameters}
97 * @return {@link WriterParameters}
100 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
101 @Nullable final UriInfo uriInfo, final boolean tagged) {
102 return parseParams(identifier, uriInfo, tagged);
106 * Parse parameters from URI request and check their types and values.
110 * {@link InstanceIdentifierContext}
113 * @return {@link WriterParameters}
116 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
117 @Nullable final UriInfo uriInfo) {
118 return parseParams(identifier, uriInfo, false);
121 private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
122 final boolean tagged) {
123 final WriterParametersBuilder builder = new WriterParametersBuilder();
124 builder.setTagged(tagged);
126 if (uriInfo == null) {
127 return builder.build();
130 // check only allowed parameters
131 ParametersUtil.checkParametersTypes(
132 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
133 uriInfo.getQueryParameters().keySet(),
134 RestconfDataServiceConstant.ReadData.CONTENT,
135 RestconfDataServiceConstant.ReadData.DEPTH,
136 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
138 // read parameters from URI or set default values
139 final List<String> content = uriInfo.getQueryParameters().getOrDefault(
140 RestconfDataServiceConstant.ReadData.CONTENT,
141 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
142 final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
143 RestconfDataServiceConstant.ReadData.DEPTH,
144 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
146 final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
147 RestconfDataServiceConstant.ReadData.FIELDS,
148 Collections.emptyList());
150 // parameter can be in URI at most once
151 ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
152 ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
153 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
155 // check and set content
156 final String contentValue = content.get(0);
157 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
158 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
159 && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
160 throw new RestconfDocumentedException(
161 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
162 "Invalid content parameter: " + contentValue, null,
163 "The content parameter value must be either config, nonconfig or all (default)"));
167 builder.setContent(content.get(0));
169 // check and set depth
170 if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
171 final Integer value = Ints.tryParse(depth.get(0));
174 || (!((value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH)
175 && (value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)))) {
176 throw new RestconfDocumentedException(
177 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
178 "Invalid depth parameter: " + depth, null,
179 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
181 builder.setDepth(value);
185 // check and set fields
186 if (!fields.isEmpty()) {
187 builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
190 return builder.build();
194 * Read specific type of data from data store via transaction.
196 * @param valueOfContent
197 * type of data to read (config, state, all)
198 * @param transactionNode
199 * {@link TransactionVarsWrapper} - wrapper for variables
200 * @param schemaContext
202 * @return {@link NormalizedNode}
205 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
206 @Nonnull final TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
207 return readData(valueOfContent, transactionNode, null, schemaContext);
211 * Read specific type of data from data store via transaction.
213 * @param valueOfContent
214 * type of data to read (config, state, all)
215 * @param transactionNode
216 * {@link TransactionVarsWrapper} - wrapper for variables
218 * vaule of with-defaults parameter
221 * @return {@link NormalizedNode}
224 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
225 @Nonnull final TransactionVarsWrapper transactionNode, final String withDefa, final SchemaContext ctx) {
226 switch (valueOfContent) {
227 case RestconfDataServiceConstant.ReadData.CONFIG:
228 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
229 if (withDefa == null) {
230 return readDataViaTransaction(transactionNode);
232 return prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
233 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
235 case RestconfDataServiceConstant.ReadData.NONCONFIG:
236 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
237 return readDataViaTransaction(transactionNode);
239 case RestconfDataServiceConstant.ReadData.ALL:
240 return readAllData(transactionNode, withDefa, ctx);
243 throw new RestconfDocumentedException(
244 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
245 "Invalid content parameter: " + valueOfContent, null,
246 "The content parameter value must be either config, nonconfig or all (default)"));
251 * Read specific type of data from data store via transaction and if identifier read data from
252 * streams then put streams from actual schema context to datastore.
255 * identifier of data to read
257 * type of data to read (config, state, all)
258 * @param transactionNode
259 * {@link TransactionVarsWrapper} - wrapper for variables
261 * vaule of with-defaults parameter
262 * @param schemaContextRef
266 * @return {@link NormalizedNode}
268 public static NormalizedNode<?, ?> readData(final String identifier, final String content,
269 final TransactionVarsWrapper transactionNode, final String withDefa,
270 final SchemaContextRef schemaContextRef, final UriInfo uriInfo) {
271 final SchemaContext schemaContext = schemaContextRef.get();
272 if (identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
273 final DOMDataReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
274 final boolean exist = SubscribeToStreamUtil.checkExist(schemaContext, wTx);
276 for (final NotificationDefinition notificationDefinition : schemaContextRef.get().getNotifications()) {
277 final List<NotificationListenerAdapter> notifiStreamXML =
278 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
279 NotificationOutputType.XML.getName());
280 final List<NotificationListenerAdapter> notifiStreamJSON =
281 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
282 NotificationOutputType.JSON.getName());
283 notifiStreamJSON.addAll(notifiStreamXML);
285 for (final NotificationListenerAdapter listener : notifiStreamJSON) {
286 final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
287 final NormalizedNode mapToStreams =
288 RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
289 listener.getSchemaPath().getLastComponent(), schemaContext.getNotifications(),
290 null, listener.getOutputType(), uri,
291 SubscribeToStreamUtil.getMonitoringModule(schemaContext), exist);
292 SubscribeToStreamUtil.writeDataToDS(schemaContext,
293 listener.getSchemaPath().getLastComponent().getLocalName(), wTx, exist,
297 SubscribeToStreamUtil.submitData(wTx);
299 return readData(content, transactionNode, withDefa, schemaContext);
302 private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
303 final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
313 throw new RestconfDocumentedException("");
316 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
317 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
318 if (result instanceof ContainerNode) {
319 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
320 Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
321 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
322 return builder.build();
324 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
325 Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
326 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
327 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
328 return builder.build();
332 private static void buildMapEntryBuilder(
333 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
334 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
335 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
336 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
337 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
338 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
339 if (child instanceof ContainerNode) {
340 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> childBuilder =
341 Builders.containerBuilder((ContainerSchemaNode) childSchema);
342 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
343 builder.withChild(childBuilder.build());
344 } else if (child instanceof MapNode) {
345 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
346 Builders.mapBuilder((ListSchemaNode) childSchema);
347 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
348 ((ListSchemaNode) childSchema).getKeyDefinition());
349 builder.withChild(childBuilder.build());
350 } else if (child instanceof LeafNode) {
351 final String defaultVal = ((LeafSchemaNode) childSchema).getDefault();
352 final String nodeVal = ((LeafNode<String>) child).getValue();
353 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
354 Builders.leafBuilder((LeafSchemaNode) childSchema);
355 if (keys.contains(child.getNodeType())) {
356 leafBuilder.withValue(((LeafNode) child).getValue());
357 builder.withChild(leafBuilder.build());
360 if ((defaultVal == null) || !defaultVal.equals(nodeVal)) {
361 leafBuilder.withValue(((LeafNode) child).getValue());
362 builder.withChild(leafBuilder.build());
365 if ((defaultVal != null) && defaultVal.equals(nodeVal)) {
366 leafBuilder.withValue(((LeafNode) child).getValue());
367 builder.withChild(leafBuilder.build());
375 private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
376 final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
377 final List<QName> keys) {
378 for (final MapEntryNode mapEntryNode : result.getValue()) {
379 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
380 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
381 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
382 Builders.mapEntryBuilder((ListSchemaNode) childSchema);
383 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
384 builder.withChild(mapEntryBuilder.build());
388 private static void buildCont(final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder,
389 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
390 final YangInstanceIdentifier actualPath, final boolean trim) {
391 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
392 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
393 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
394 if (child instanceof ContainerNode) {
395 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builderChild =
396 Builders.containerBuilder((ContainerSchemaNode) childSchema);
397 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
398 builder.withChild(builderChild.build());
399 } else if (child instanceof MapNode) {
400 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
401 Builders.mapBuilder((ListSchemaNode) childSchema);
402 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
403 ((ListSchemaNode) childSchema).getKeyDefinition());
404 builder.withChild(childBuilder.build());
405 } else if (child instanceof LeafNode) {
406 final String defaultVal = ((LeafSchemaNode) childSchema).getDefault();
407 final String nodeVal = ((LeafNode<String>) child).getValue();
408 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
409 Builders.leafBuilder((LeafSchemaNode) childSchema);
411 if ((defaultVal == null) || !defaultVal.equals(nodeVal)) {
412 leafBuilder.withValue(((LeafNode) child).getValue());
413 builder.withChild(leafBuilder.build());
416 if ((defaultVal != null) && defaultVal.equals(nodeVal)) {
417 leafBuilder.withValue(((LeafNode) child).getValue());
418 builder.withChild(leafBuilder.build());
426 * If is set specific {@link LogicalDatastoreType} in
427 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
428 * don't, we have to read all data from DS (state + config)
430 * @param transactionNode
431 * {@link TransactionVarsWrapper} - wrapper for variables
432 * @return {@link NormalizedNode}
435 private static NormalizedNode<?, ?> readDataViaTransaction(
436 @Nonnull final TransactionVarsWrapper transactionNode) {
437 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
438 try (DOMDataReadOnlyTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
439 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> listenableFuture = tx.read(
440 transactionNode.getLogicalDatastoreType(),
441 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
442 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
445 return dataFactory.build();
449 * Read config and state data, then map them.
451 * @param transactionNode
452 * {@link TransactionVarsWrapper} - wrapper for variables
454 * with-defaults parameter
457 * @return {@link NormalizedNode}
460 private static NormalizedNode<?, ?> readAllData(@Nonnull final TransactionVarsWrapper transactionNode,
461 final String withDefa, final SchemaContext ctx) {
462 // PREPARE STATE DATA NODE
463 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
464 final NormalizedNode<?, ?> stateDataNode = readDataViaTransaction(transactionNode);
466 // PREPARE CONFIG DATA NODE
467 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
468 final NormalizedNode<?, ?> configDataNode;
469 if (withDefa == null) {
470 configDataNode = readDataViaTransaction(transactionNode);
472 configDataNode = prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
473 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
477 if ((stateDataNode == null) && (configDataNode == null)) {
481 // return config data
482 if (stateDataNode == null) {
483 return configDataNode;
487 if (configDataNode == null) {
488 return stateDataNode;
491 // merge data from config and state
492 return mapNode(stateDataNode, configDataNode);
496 * Map data by type of read node.
498 * @param stateDataNode
499 * data node of state data
500 * @param configDataNode
501 * data node of config data
502 * @return {@link NormalizedNode}
505 private static NormalizedNode<?, ?> mapNode(@Nonnull final NormalizedNode<?, ?> stateDataNode,
506 @Nonnull final NormalizedNode<?, ?> configDataNode) {
507 validPossibilityOfMergeNodes(stateDataNode, configDataNode);
508 if (configDataNode instanceof RpcDefinition) {
509 return prepareRpcData(configDataNode, stateDataNode);
511 return prepareData(configDataNode, stateDataNode);
516 * Valid of can be data merged together.
518 * @param stateDataNode
519 * data node of state data
520 * @param configDataNode
521 * data node of config data
523 private static void validPossibilityOfMergeNodes(@Nonnull final NormalizedNode<?, ?> stateDataNode,
524 @Nonnull final NormalizedNode<?, ?> configDataNode) {
525 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
526 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
527 if (moduleOfStateData != moduleOfConfigData) {
528 throw new RestconfDocumentedException("It is not possible to merge ");
533 * Prepare and map data for rpc.
535 * @param configDataNode
536 * data node of config data
537 * @param stateDataNode
538 * data node of state data
539 * @return {@link NormalizedNode}
542 private static NormalizedNode<?, ?> prepareRpcData(@Nonnull final NormalizedNode<?, ?> configDataNode,
543 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
544 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
546 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
549 mapRpcDataNode(configDataNode, mapEntryBuilder);
551 mapRpcDataNode(stateDataNode, mapEntryBuilder);
553 return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
557 * Map node to map entry builder.
561 * @param mapEntryBuilder
562 * builder for mapping data
564 private static void mapRpcDataNode(@Nonnull final NormalizedNode<?, ?> dataNode,
565 @Nonnull final DataContainerNodeBuilder<
566 NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
567 ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
571 * Prepare and map all data from DS.
573 * @param configDataNode
574 * data node of config data
575 * @param stateDataNode
576 * data node of state data
577 * @return {@link NormalizedNode}
580 private static NormalizedNode<?, ?> prepareData(@Nonnull final NormalizedNode<?, ?> configDataNode,
581 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
582 if (configDataNode instanceof MapNode) {
583 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
584 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
587 ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
589 return builder.build();
590 } else if (configDataNode instanceof MapEntryNode) {
591 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
592 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
595 ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
597 return builder.build();
598 } else if (configDataNode instanceof ContainerNode) {
599 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = Builders
600 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
603 ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
605 return builder.build();
606 } else if (configDataNode instanceof AugmentationNode) {
607 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
608 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
610 mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
611 ((AugmentationNode) stateDataNode).getValue(), builder);
613 return builder.build();
614 } else if (configDataNode instanceof ChoiceNode) {
615 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
616 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
619 ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
621 return builder.build();
622 } else if (configDataNode instanceof LeafNode) {
623 return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
625 throw new RestconfDocumentedException("Bad type of node.");
630 * Map value from container node to builder.
633 * collection of config data nodes
635 * collection of state data nodes
639 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
640 @Nonnull final Collection<T> configData,
641 @Nonnull final Collection<T> stateData,
642 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
643 final Map<PathArgument, T> configMap = configData.stream().collect(
644 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
645 final Map<PathArgument, T> stateMap = stateData.stream().collect(
646 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
648 // merge config and state data of children with different identifiers
649 mapDataToBuilder(configMap, stateMap, builder);
651 // merge config and state data of children with the same identifiers
652 mergeDataToBuilder(configMap, stateMap, builder);
656 * Map data with different identifiers to builder. Data with different identifiers can be just added
657 * as childs to parent node.
660 * map of config data nodes
662 * map of state data nodes
666 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
667 @Nonnull final Map<PathArgument, T> configMap,
668 @Nonnull final Map<PathArgument, T> stateMap,
669 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
670 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
671 y -> builder.addChild(y.getValue()));
672 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
673 y -> builder.addChild(y.getValue()));
677 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
678 * go one level down with {@code prepareData} method.
681 * immutable config data
683 * immutable state data
687 @SuppressWarnings("unchecked")
688 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
689 @Nonnull final Map<PathArgument, T> configMap,
690 @Nonnull final Map<PathArgument, T> stateMap,
691 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
692 // it is enough to process only config data because operational contains the same data
693 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
694 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));