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.collect.Iterables;
15 import com.google.common.primitives.Ints;
16 import com.google.common.util.concurrent.CheckedFuture;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
22 import java.util.function.Function;
23 import java.util.stream.Collectors;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.ws.rs.core.UriInfo;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.restconf.common.context.WriterParameters;
33 import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder;
34 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
35 import org.opendaylight.restconf.common.errors.RestconfError;
36 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
38 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
39 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
40 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserFieldsParameter;
41 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.QNameModule;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
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.impl.schema.Builders;
58 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
62 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
63 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
64 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
65 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
66 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
67 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
69 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
70 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
71 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
74 * Util class for read data from data store via transaction.
78 * <li>all (config + state)
82 public final class ReadDataTransactionUtil {
84 private ReadDataTransactionUtil() {
85 throw new UnsupportedOperationException("Util class.");
89 * Parse parameters from URI request and check their types and values.
93 * {@link InstanceIdentifierContext}
97 * set tagged for {@link WriterParameters}
98 * @return {@link WriterParameters}
101 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
102 @Nullable final UriInfo uriInfo, final boolean tagged) {
103 return parseParams(identifier, uriInfo, tagged);
107 * Parse parameters from URI request and check their types and values.
111 * {@link InstanceIdentifierContext}
114 * @return {@link WriterParameters}
117 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
118 @Nullable final UriInfo uriInfo) {
119 return parseParams(identifier, uriInfo, false);
122 private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
123 final boolean tagged) {
124 final WriterParametersBuilder builder = new WriterParametersBuilder();
125 builder.setTagged(tagged);
127 if (uriInfo == null) {
128 return builder.build();
131 // check only allowed parameters
132 ParametersUtil.checkParametersTypes(
133 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
134 uriInfo.getQueryParameters().keySet(),
135 RestconfDataServiceConstant.ReadData.CONTENT,
136 RestconfDataServiceConstant.ReadData.DEPTH,
137 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
139 // read parameters from URI or set default values
140 final List<String> content = uriInfo.getQueryParameters().getOrDefault(
141 RestconfDataServiceConstant.ReadData.CONTENT,
142 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
143 final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
144 RestconfDataServiceConstant.ReadData.DEPTH,
145 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
147 final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
148 RestconfDataServiceConstant.ReadData.FIELDS,
149 Collections.emptyList());
151 // parameter can be in URI at most once
152 ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
153 ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
154 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
156 // check and set content
157 final String contentValue = content.get(0);
158 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
159 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
160 && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
161 throw new RestconfDocumentedException(
162 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
163 "Invalid content parameter: " + contentValue, null,
164 "The content parameter value must be either config, nonconfig or all (default)"));
168 builder.setContent(content.get(0));
170 // check and set depth
171 if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
172 final Integer value = Ints.tryParse(depth.get(0));
175 || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
176 && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
177 throw new RestconfDocumentedException(
178 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
179 "Invalid depth parameter: " + depth, null,
180 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
182 builder.setDepth(value);
186 // check and set fields
187 if (!fields.isEmpty()) {
188 builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
191 return builder.build();
195 * Read specific type of data from data store via transaction.
197 * @param valueOfContent
198 * type of data to read (config, state, all)
199 * @param transactionNode
200 * {@link TransactionVarsWrapper} - wrapper for variables
201 * @param schemaContext
203 * @return {@link NormalizedNode}
206 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
207 @Nonnull final TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
208 return readData(valueOfContent, transactionNode, null, schemaContext);
212 * Read specific type of data from data store via transaction.
214 * @param valueOfContent
215 * type of data to read (config, state, all)
216 * @param transactionNode
217 * {@link TransactionVarsWrapper} - wrapper for variables
219 * vaule of with-defaults parameter
222 * @return {@link NormalizedNode}
225 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
226 @Nonnull final 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.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
274 final DOMDataReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
275 final boolean exist = SubscribeToStreamUtil.checkExist(schemaContext, wTx);
277 for (final NotificationDefinition notificationDefinition : schemaContextRef.get().getNotifications()) {
278 final List<NotificationListenerAdapter> notifiStreamXML =
279 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
280 NotificationOutputType.XML.getName());
281 final List<NotificationListenerAdapter> notifiStreamJSON =
282 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
283 NotificationOutputType.JSON.getName());
284 for (final NotificationListenerAdapter listener : Iterables.concat(notifiStreamXML, notifiStreamJSON)) {
285 final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
286 final NormalizedNode mapToStreams =
287 RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
288 listener.getSchemaPath().getLastComponent(), schemaContext.getNotifications(),
289 null, listener.getOutputType(), uri,
290 SubscribeToStreamUtil.getMonitoringModule(schemaContext), exist);
291 SubscribeToStreamUtil.writeDataToDS(schemaContext,
292 listener.getSchemaPath().getLastComponent().getLocalName(), wTx, exist,
296 SubscribeToStreamUtil.submitData(wTx);
298 return readData(content, transactionNode, withDefa, schemaContext);
301 private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
302 final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
312 throw new RestconfDocumentedException("");
315 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
316 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
317 if (result instanceof ContainerNode) {
318 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
319 Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
320 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
321 return builder.build();
323 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
324 Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
325 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
326 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
327 return builder.build();
331 private static void buildMapEntryBuilder(
332 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
333 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
334 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
335 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
336 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
337 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
338 if (child instanceof ContainerNode) {
339 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> childBuilder =
340 Builders.containerBuilder((ContainerSchemaNode) childSchema);
341 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
342 builder.withChild(childBuilder.build());
343 } else if (child instanceof MapNode) {
344 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
345 Builders.mapBuilder((ListSchemaNode) childSchema);
346 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
347 ((ListSchemaNode) childSchema).getKeyDefinition());
348 builder.withChild(childBuilder.build());
349 } else if (child instanceof LeafNode) {
350 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
351 final Object nodeVal = ((LeafNode<?>) child).getValue();
352 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
353 Builders.leafBuilder((LeafSchemaNode) childSchema);
354 if (keys.contains(child.getNodeType())) {
355 leafBuilder.withValue(((LeafNode) child).getValue());
356 builder.withChild(leafBuilder.build());
359 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
360 leafBuilder.withValue(((LeafNode) child).getValue());
361 builder.withChild(leafBuilder.build());
364 if (defaultVal != null && defaultVal.equals(nodeVal)) {
365 leafBuilder.withValue(((LeafNode) child).getValue());
366 builder.withChild(leafBuilder.build());
374 private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
375 final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
376 final List<QName> keys) {
377 for (final MapEntryNode mapEntryNode : result.getValue()) {
378 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
379 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
380 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
381 Builders.mapEntryBuilder((ListSchemaNode) childSchema);
382 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
383 builder.withChild(mapEntryBuilder.build());
387 private static void buildCont(final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder,
388 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
389 final YangInstanceIdentifier actualPath, final boolean trim) {
390 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
391 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
392 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
393 if (child instanceof ContainerNode) {
394 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builderChild =
395 Builders.containerBuilder((ContainerSchemaNode) childSchema);
396 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
397 builder.withChild(builderChild.build());
398 } else if (child instanceof MapNode) {
399 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
400 Builders.mapBuilder((ListSchemaNode) childSchema);
401 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
402 ((ListSchemaNode) childSchema).getKeyDefinition());
403 builder.withChild(childBuilder.build());
404 } else if (child instanceof LeafNode) {
405 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
406 final Object nodeVal = ((LeafNode<?>) child).getValue();
407 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
408 Builders.leafBuilder((LeafSchemaNode) childSchema);
410 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
411 leafBuilder.withValue(((LeafNode) child).getValue());
412 builder.withChild(leafBuilder.build());
415 if (defaultVal != null && defaultVal.equals(nodeVal)) {
416 leafBuilder.withValue(((LeafNode) child).getValue());
417 builder.withChild(leafBuilder.build());
425 * If is set specific {@link LogicalDatastoreType} in
426 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
427 * don't, we have to read all data from DS (state + config)
429 * @param transactionNode
430 * {@link TransactionVarsWrapper} - wrapper for variables
431 * @return {@link NormalizedNode}
434 private static NormalizedNode<?, ?> readDataViaTransaction(
435 @Nonnull final TransactionVarsWrapper transactionNode) {
436 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
437 try (DOMDataReadOnlyTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
438 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> listenableFuture = tx.read(
439 transactionNode.getLogicalDatastoreType(),
440 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
441 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
444 return dataFactory.build();
448 * Read config and state data, then map them.
450 * @param transactionNode
451 * {@link TransactionVarsWrapper} - wrapper for variables
453 * with-defaults parameter
456 * @return {@link NormalizedNode}
459 private static NormalizedNode<?, ?> readAllData(@Nonnull final TransactionVarsWrapper transactionNode,
460 final String withDefa, final SchemaContext ctx) {
461 // PREPARE STATE DATA NODE
462 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
463 final NormalizedNode<?, ?> stateDataNode = readDataViaTransaction(transactionNode);
465 // PREPARE CONFIG DATA NODE
466 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
467 final NormalizedNode<?, ?> configDataNode;
468 if (withDefa == null) {
469 configDataNode = readDataViaTransaction(transactionNode);
471 configDataNode = prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
472 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
476 if (stateDataNode == null && configDataNode == null) {
480 // return config data
481 if (stateDataNode == null) {
482 return configDataNode;
486 if (configDataNode == null) {
487 return stateDataNode;
490 // merge data from config and state
491 return mapNode(stateDataNode, configDataNode);
495 * Map data by type of read node.
497 * @param stateDataNode
498 * data node of state data
499 * @param configDataNode
500 * data node of config data
501 * @return {@link NormalizedNode}
504 private static NormalizedNode<?, ?> mapNode(@Nonnull final NormalizedNode<?, ?> stateDataNode,
505 @Nonnull final NormalizedNode<?, ?> configDataNode) {
506 validPossibilityOfMergeNodes(stateDataNode, configDataNode);
507 if (configDataNode instanceof RpcDefinition) {
508 return prepareRpcData(configDataNode, stateDataNode);
510 return prepareData(configDataNode, stateDataNode);
515 * Valid of can be data merged together.
517 * @param stateDataNode
518 * data node of state data
519 * @param configDataNode
520 * data node of config data
522 private static void validPossibilityOfMergeNodes(@Nonnull final NormalizedNode<?, ?> stateDataNode,
523 @Nonnull final NormalizedNode<?, ?> configDataNode) {
524 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
525 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
526 if (moduleOfStateData != moduleOfConfigData) {
527 throw new RestconfDocumentedException("It is not possible to merge ");
532 * Prepare and map data for rpc.
534 * @param configDataNode
535 * data node of config data
536 * @param stateDataNode
537 * data node of state data
538 * @return {@link NormalizedNode}
541 private static NormalizedNode<?, ?> prepareRpcData(@Nonnull final NormalizedNode<?, ?> configDataNode,
542 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
543 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
545 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
548 mapRpcDataNode(configDataNode, mapEntryBuilder);
550 mapRpcDataNode(stateDataNode, mapEntryBuilder);
552 return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
556 * Map node to map entry builder.
560 * @param mapEntryBuilder
561 * builder for mapping data
563 private static void mapRpcDataNode(@Nonnull final NormalizedNode<?, ?> dataNode,
564 @Nonnull final DataContainerNodeBuilder<
565 NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
566 ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
570 * Prepare and map all data from DS.
572 * @param configDataNode
573 * data node of config data
574 * @param stateDataNode
575 * data node of state data
576 * @return {@link NormalizedNode}
579 private static NormalizedNode<?, ?> prepareData(@Nonnull final NormalizedNode<?, ?> configDataNode,
580 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
581 if (configDataNode instanceof MapNode) {
582 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
583 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
586 ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
588 return builder.build();
589 } else if (configDataNode instanceof MapEntryNode) {
590 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
591 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
594 ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
596 return builder.build();
597 } else if (configDataNode instanceof ContainerNode) {
598 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = Builders
599 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
602 ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
604 return builder.build();
605 } else if (configDataNode instanceof AugmentationNode) {
606 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
607 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
609 mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
610 ((AugmentationNode) stateDataNode).getValue(), builder);
612 return builder.build();
613 } else if (configDataNode instanceof ChoiceNode) {
614 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
615 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
618 ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
620 return builder.build();
621 } else if (configDataNode instanceof LeafNode) {
622 return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
624 throw new RestconfDocumentedException("Bad type of node.");
629 * Map value from container node to builder.
632 * collection of config data nodes
634 * collection of state data nodes
638 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
639 @Nonnull final Collection<T> configData,
640 @Nonnull final Collection<T> stateData,
641 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
642 final Map<PathArgument, T> configMap = configData.stream().collect(
643 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
644 final Map<PathArgument, T> stateMap = stateData.stream().collect(
645 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
647 // merge config and state data of children with different identifiers
648 mapDataToBuilder(configMap, stateMap, builder);
650 // merge config and state data of children with the same identifiers
651 mergeDataToBuilder(configMap, stateMap, builder);
655 * Map data with different identifiers to builder. Data with different identifiers can be just added
656 * as childs to parent node.
659 * map of config data nodes
661 * map of state data nodes
665 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
666 @Nonnull final Map<PathArgument, T> configMap,
667 @Nonnull final Map<PathArgument, T> stateMap,
668 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
669 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
670 y -> builder.addChild(y.getValue()));
671 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
672 y -> builder.addChild(y.getValue()));
676 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
677 * go one level down with {@code prepareData} method.
680 * immutable config data
682 * immutable state data
686 @SuppressWarnings("unchecked")
687 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
688 @Nonnull final Map<PathArgument, T> configMap,
689 @Nonnull final Map<PathArgument, T> stateMap,
690 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
691 // it is enough to process only config data because operational contains the same data
692 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
693 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));