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.collect.Iterables;
14 import com.google.common.primitives.Ints;
15 import com.google.common.util.concurrent.FluentFuture;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
21 import java.util.Optional;
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.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
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.LeafSetEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
62 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
63 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
64 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
65 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
66 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
67 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
68 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
70 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
71 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
72 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
76 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
77 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
80 * Util class for read data from data store via transaction.
84 * <li>all (config + state)
88 public final class ReadDataTransactionUtil {
90 private ReadDataTransactionUtil() {
91 throw new UnsupportedOperationException("Util class.");
95 * Parse parameters from URI request and check their types and values.
99 * {@link InstanceIdentifierContext}
103 * set tagged for {@link WriterParameters}
104 * @return {@link WriterParameters}
107 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
108 @Nullable final UriInfo uriInfo, final boolean tagged) {
109 return parseParams(identifier, uriInfo, tagged);
113 * Parse parameters from URI request and check their types and values.
117 * {@link InstanceIdentifierContext}
120 * @return {@link WriterParameters}
123 public static WriterParameters parseUriParameters(@Nonnull final InstanceIdentifierContext<?> identifier,
124 @Nullable final UriInfo uriInfo) {
125 return parseParams(identifier, uriInfo, false);
128 private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
129 final boolean tagged) {
130 final WriterParametersBuilder builder = new WriterParametersBuilder();
131 builder.setTagged(tagged);
133 if (uriInfo == null) {
134 return builder.build();
137 // check only allowed parameters
138 ParametersUtil.checkParametersTypes(
139 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
140 uriInfo.getQueryParameters().keySet(),
141 RestconfDataServiceConstant.ReadData.CONTENT,
142 RestconfDataServiceConstant.ReadData.DEPTH,
143 RestconfDataServiceConstant.ReadData.FIELDS, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
145 // read parameters from URI or set default values
146 final List<String> content = uriInfo.getQueryParameters().getOrDefault(
147 RestconfDataServiceConstant.ReadData.CONTENT,
148 Collections.singletonList(RestconfDataServiceConstant.ReadData.ALL));
149 final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
150 RestconfDataServiceConstant.ReadData.DEPTH,
151 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
153 final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
154 RestconfDataServiceConstant.ReadData.FIELDS,
155 Collections.emptyList());
157 // parameter can be in URI at most once
158 ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
159 ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
160 ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
162 // check and set content
163 final String contentValue = content.get(0);
164 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
165 if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
166 && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
167 throw new RestconfDocumentedException(
168 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
169 "Invalid content parameter: " + contentValue, null,
170 "The content parameter value must be either config, nonconfig or all (default)"));
174 builder.setContent(content.get(0));
176 // check and set depth
177 if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
178 final Integer value = Ints.tryParse(depth.get(0));
181 || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
182 && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
183 throw new RestconfDocumentedException(
184 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
185 "Invalid depth parameter: " + depth, null,
186 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
188 builder.setDepth(value);
192 // check and set fields
193 if (!fields.isEmpty()) {
194 builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
197 return builder.build();
201 * Read specific type of data from data store via transaction.
203 * @param valueOfContent
204 * type of data to read (config, state, all)
205 * @param transactionNode
206 * {@link TransactionVarsWrapper} - wrapper for variables
207 * @param schemaContext
209 * @return {@link NormalizedNode}
212 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
213 @Nonnull final TransactionVarsWrapper transactionNode, final SchemaContext schemaContext) {
214 return readData(valueOfContent, transactionNode, null, schemaContext);
218 * Read specific type of data from data store via transaction.
220 * @param valueOfContent
221 * type of data to read (config, state, all)
222 * @param transactionNode
223 * {@link TransactionVarsWrapper} - wrapper for variables
225 * vaule of with-defaults parameter
228 * @return {@link NormalizedNode}
231 public static NormalizedNode<?, ?> readData(@Nonnull final String valueOfContent,
232 @Nonnull final TransactionVarsWrapper transactionNode, final String withDefa, final SchemaContext ctx) {
233 switch (valueOfContent) {
234 case RestconfDataServiceConstant.ReadData.CONFIG:
235 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.CONFIGURATION);
236 if (withDefa == null) {
237 return readDataViaTransaction(transactionNode);
239 return prepareDataByParamWithDef(readDataViaTransaction(transactionNode),
240 transactionNode.getInstanceIdentifier().getInstanceIdentifier(), withDefa, ctx);
242 case RestconfDataServiceConstant.ReadData.NONCONFIG:
243 transactionNode.setLogicalDatastoreType(LogicalDatastoreType.OPERATIONAL);
244 return readDataViaTransaction(transactionNode);
246 case RestconfDataServiceConstant.ReadData.ALL:
247 return readAllData(transactionNode, withDefa, ctx);
250 throw new RestconfDocumentedException(
251 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
252 "Invalid content parameter: " + valueOfContent, null,
253 "The content parameter value must be either config, nonconfig or all (default)"));
258 * Read specific type of data from data store via transaction and if identifier read data from
259 * streams then put streams from actual schema context to datastore.
262 * identifier of data to read
264 * type of data to read (config, state, all)
265 * @param transactionNode
266 * {@link TransactionVarsWrapper} - wrapper for variables
268 * vaule of with-defaults parameter
269 * @param schemaContextRef
273 * @return {@link NormalizedNode}
275 public static NormalizedNode<?, ?> readData(final String identifier, final String content,
276 final TransactionVarsWrapper transactionNode, final String withDefa,
277 final SchemaContextRef schemaContextRef, final UriInfo uriInfo) {
278 final SchemaContext schemaContext = schemaContextRef.get();
279 if (identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
280 final DOMDataTreeReadWriteTransaction wTx = transactionNode.getTransactionChain().newReadWriteTransaction();
281 final boolean exist = SubscribeToStreamUtil.checkExist(schemaContext, wTx);
283 for (final NotificationDefinition notificationDefinition : schemaContextRef.get().getNotifications()) {
284 final List<NotificationListenerAdapter> notifiStreamXML =
285 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
286 NotificationOutputType.XML.getName());
287 final List<NotificationListenerAdapter> notifiStreamJSON =
288 CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
289 NotificationOutputType.JSON.getName());
290 for (final NotificationListenerAdapter listener : Iterables.concat(notifiStreamXML, notifiStreamJSON)) {
291 final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
292 final NormalizedNode mapToStreams =
293 RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
294 listener.getSchemaPath().getLastComponent(), schemaContext.getNotifications(),
295 null, listener.getOutputType(), uri,
296 SubscribeToStreamUtil.getMonitoringModule(schemaContext), exist);
297 SubscribeToStreamUtil.writeDataToDS(schemaContext,
298 listener.getSchemaPath().getLastComponent().getLocalName(), wTx, exist,
302 SubscribeToStreamUtil.submitData(wTx);
304 return readData(content, transactionNode, withDefa, schemaContext);
307 private static NormalizedNode<?, ?> prepareDataByParamWithDef(final NormalizedNode<?, ?> result,
308 final YangInstanceIdentifier path, final String withDefa, final SchemaContext ctx) {
318 throw new RestconfDocumentedException("");
321 final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
322 final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
323 if (result instanceof ContainerNode) {
324 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
325 Builders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
326 buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
327 return builder.build();
329 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
330 Builders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
331 buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim,
332 ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
333 return builder.build();
337 private static void buildMapEntryBuilder(
338 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder,
339 final MapEntryNode result, final DataSchemaContextTree baseSchemaCtxTree,
340 final YangInstanceIdentifier actualPath, final boolean trim, final List<QName> keys) {
341 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
342 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
343 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
344 if (child instanceof ContainerNode) {
345 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> childBuilder =
346 Builders.containerBuilder((ContainerSchemaNode) childSchema);
347 buildCont(childBuilder, (ContainerNode) child, baseSchemaCtxTree, path, trim);
348 builder.withChild(childBuilder.build());
349 } else if (child instanceof MapNode) {
350 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
351 Builders.mapBuilder((ListSchemaNode) childSchema);
352 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
353 ((ListSchemaNode) childSchema).getKeyDefinition());
354 builder.withChild(childBuilder.build());
355 } else if (child instanceof LeafNode) {
356 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
357 final Object nodeVal = child.getValue();
358 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
359 Builders.leafBuilder((LeafSchemaNode) childSchema);
360 if (keys.contains(child.getNodeType())) {
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());
370 if (defaultVal != null && defaultVal.equals(nodeVal)) {
371 leafBuilder.withValue(((LeafNode) child).getValue());
372 builder.withChild(leafBuilder.build());
380 private static void buildList(final CollectionNodeBuilder<MapEntryNode, MapNode> builder, final MapNode result,
381 final DataSchemaContextTree baseSchemaCtxTree, final YangInstanceIdentifier path, final boolean trim,
382 final List<QName> keys) {
383 for (final MapEntryNode mapEntryNode : result.getValue()) {
384 final YangInstanceIdentifier actualNode = path.node(mapEntryNode.getIdentifier());
385 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(actualNode).getDataSchemaNode();
386 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder =
387 Builders.mapEntryBuilder((ListSchemaNode) childSchema);
388 buildMapEntryBuilder(mapEntryBuilder, mapEntryNode, baseSchemaCtxTree, actualNode, trim, keys);
389 builder.withChild(mapEntryBuilder.build());
393 private static void buildCont(final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder,
394 final ContainerNode result, final DataSchemaContextTree baseSchemaCtxTree,
395 final YangInstanceIdentifier actualPath, final boolean trim) {
396 for (final DataContainerChild<? extends PathArgument, ?> child : result.getValue()) {
397 final YangInstanceIdentifier path = actualPath.node(child.getIdentifier());
398 final DataSchemaNode childSchema = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
399 if (child instanceof ContainerNode) {
400 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builderChild =
401 Builders.containerBuilder((ContainerSchemaNode) childSchema);
402 buildCont(builderChild, result, baseSchemaCtxTree, actualPath, trim);
403 builder.withChild(builderChild.build());
404 } else if (child instanceof MapNode) {
405 final CollectionNodeBuilder<MapEntryNode, MapNode> childBuilder =
406 Builders.mapBuilder((ListSchemaNode) childSchema);
407 buildList(childBuilder, (MapNode) child, baseSchemaCtxTree, path, trim,
408 ((ListSchemaNode) childSchema).getKeyDefinition());
409 builder.withChild(childBuilder.build());
410 } else if (child instanceof LeafNode) {
411 final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
412 final Object nodeVal = child.getValue();
413 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
414 Builders.leafBuilder((LeafSchemaNode) childSchema);
416 if (defaultVal == null || !defaultVal.equals(nodeVal)) {
417 leafBuilder.withValue(((LeafNode) child).getValue());
418 builder.withChild(leafBuilder.build());
421 if (defaultVal != null && defaultVal.equals(nodeVal)) {
422 leafBuilder.withValue(((LeafNode) child).getValue());
423 builder.withChild(leafBuilder.build());
431 * If is set specific {@link LogicalDatastoreType} in
432 * {@link TransactionVarsWrapper}, then read this type of data from DS. If
433 * don't, we have to read all data from DS (state + config)
435 * @param transactionNode
436 * {@link TransactionVarsWrapper} - wrapper for variables
437 * @return {@link NormalizedNode}
440 private static NormalizedNode<?, ?> readDataViaTransaction(
441 @Nonnull final TransactionVarsWrapper transactionNode) {
442 final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
443 try (DOMDataTreeReadTransaction tx = transactionNode.getTransactionChain().newReadOnlyTransaction()) {
444 final FluentFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = tx.read(
445 transactionNode.getLogicalDatastoreType(),
446 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
447 FutureCallbackTx.addCallback(listenableFuture, RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
450 return dataFactory.build();
454 * Read config and state data, then map them.
456 * @param transactionNode
457 * {@link TransactionVarsWrapper} - wrapper for variables
459 * with-defaults parameter
462 * @return {@link NormalizedNode}
465 private static NormalizedNode<?, ?> readAllData(@Nonnull final 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}
510 private static NormalizedNode<?, ?> mergeStateAndConfigData(@Nonnull final NormalizedNode<?, ?> stateDataNode,
511 @Nonnull final NormalizedNode<?, ?> configDataNode) {
512 validateNodeMerge(stateDataNode, configDataNode);
513 if (configDataNode instanceof RpcDefinition) {
514 return prepareRpcData(configDataNode, stateDataNode);
516 return prepareData(configDataNode, stateDataNode);
521 * Validates whether the two NormalizedNodes can be merged.
523 * @param stateDataNode
524 * data node of state data
525 * @param configDataNode
526 * data node of config data
528 private static void validateNodeMerge(@Nonnull final NormalizedNode<?, ?> stateDataNode,
529 @Nonnull final NormalizedNode<?, ?> configDataNode) {
530 final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
531 final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
532 if (!moduleOfStateData.equals(moduleOfConfigData)) {
533 throw new RestconfDocumentedException("Unable to merge data from different modules.");
538 * Prepare and map data for rpc.
540 * @param configDataNode
541 * data node of config data
542 * @param stateDataNode
543 * data node of state data
544 * @return {@link NormalizedNode}
547 private static NormalizedNode<?, ?> prepareRpcData(@Nonnull final NormalizedNode<?, ?> configDataNode,
548 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
549 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = ImmutableNodes
551 mapEntryBuilder.withNodeIdentifier((NodeIdentifierWithPredicates) configDataNode.getIdentifier());
554 mapRpcDataNode(configDataNode, mapEntryBuilder);
556 mapRpcDataNode(stateDataNode, mapEntryBuilder);
558 return ImmutableNodes.mapNodeBuilder(configDataNode.getNodeType()).addChild(mapEntryBuilder.build()).build();
562 * Map node to map entry builder.
566 * @param mapEntryBuilder
567 * builder for mapping data
569 private static void mapRpcDataNode(@Nonnull final NormalizedNode<?, ?> dataNode,
570 @Nonnull final DataContainerNodeBuilder<
571 NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder) {
572 ((ContainerNode) dataNode).getValue().forEach(mapEntryBuilder::addChild);
576 * Prepare and map all data from DS.
578 * @param configDataNode
579 * data node of config data
580 * @param stateDataNode
581 * data node of state data
582 * @return {@link NormalizedNode}
584 @SuppressWarnings("unchecked")
586 private static NormalizedNode<?, ?> prepareData(@Nonnull final NormalizedNode<?, ?> configDataNode,
587 @Nonnull final NormalizedNode<?, ?> stateDataNode) {
588 if (configDataNode instanceof OrderedMapNode) {
589 final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = Builders
590 .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
593 ((OrderedMapNode) configDataNode).getValue(), ((OrderedMapNode) stateDataNode).getValue(), builder);
595 return builder.build();
596 } else if (configDataNode instanceof MapNode) {
597 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
598 .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
601 ((MapNode) configDataNode).getValue(), ((MapNode) stateDataNode).getValue(), builder);
603 return builder.build();
604 } else if (configDataNode instanceof MapEntryNode) {
605 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = ImmutableNodes
606 .mapEntryBuilder().withNodeIdentifier(((MapEntryNode) configDataNode).getIdentifier());
609 ((MapEntryNode) configDataNode).getValue(), ((MapEntryNode) stateDataNode).getValue(), builder);
611 return builder.build();
612 } else if (configDataNode instanceof ContainerNode) {
613 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = Builders
614 .containerBuilder().withNodeIdentifier(((ContainerNode) configDataNode).getIdentifier());
617 ((ContainerNode) configDataNode).getValue(), ((ContainerNode) stateDataNode).getValue(), builder);
619 return builder.build();
620 } else if (configDataNode instanceof AugmentationNode) {
621 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> builder = Builders
622 .augmentationBuilder().withNodeIdentifier(((AugmentationNode) configDataNode).getIdentifier());
624 mapValueToBuilder(((AugmentationNode) configDataNode).getValue(),
625 ((AugmentationNode) stateDataNode).getValue(), builder);
627 return builder.build();
628 } else if (configDataNode instanceof ChoiceNode) {
629 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = Builders
630 .choiceBuilder().withNodeIdentifier(((ChoiceNode) configDataNode).getIdentifier());
633 ((ChoiceNode) configDataNode).getValue(), ((ChoiceNode) stateDataNode).getValue(), builder);
635 return builder.build();
636 } else if (configDataNode instanceof LeafNode) {
637 return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
638 } else if (configDataNode instanceof OrderedLeafSetNode) {
639 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
640 .orderedLeafSetBuilder().withNodeIdentifier(((OrderedLeafSetNode<?>) configDataNode).getIdentifier());
642 mapValueToBuilder(((OrderedLeafSetNode<Object>) configDataNode).getValue(),
643 ((OrderedLeafSetNode<Object>) stateDataNode).getValue(), builder);
644 return builder.build();
645 } else if (configDataNode instanceof LeafSetNode) {
646 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
647 .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
649 mapValueToBuilder(((LeafSetNode<Object>) configDataNode).getValue(),
650 ((LeafSetNode<Object>) stateDataNode).getValue(), builder);
651 return builder.build();
652 } else if (configDataNode instanceof UnkeyedListNode) {
653 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
654 .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
656 mapValueToBuilder(((UnkeyedListNode) configDataNode).getValue(),
657 ((UnkeyedListNode) stateDataNode).getValue(), builder);
658 return builder.build();
659 } else if (configDataNode instanceof UnkeyedListEntryNode) {
660 final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
661 .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
663 mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).getValue(),
664 ((UnkeyedListEntryNode) stateDataNode).getValue(), builder);
665 return builder.build();
667 throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
672 * Map value from container node to builder.
675 * collection of config data nodes
677 * collection of state data nodes
681 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapValueToBuilder(
682 @Nonnull final Collection<T> configData,
683 @Nonnull final Collection<T> stateData,
684 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
685 final Map<PathArgument, T> configMap = configData.stream().collect(
686 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
687 final Map<PathArgument, T> stateMap = stateData.stream().collect(
688 Collectors.toMap(NormalizedNode::getIdentifier, Function.identity()));
690 // merge config and state data of children with different identifiers
691 mapDataToBuilder(configMap, stateMap, builder);
693 // merge config and state data of children with the same identifiers
694 mergeDataToBuilder(configMap, stateMap, builder);
698 * Map data with different identifiers to builder. Data with different identifiers can be just added
699 * as childs to parent node.
702 * map of config data nodes
704 * map of state data nodes
708 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mapDataToBuilder(
709 @Nonnull final Map<PathArgument, T> configMap,
710 @Nonnull final Map<PathArgument, T> stateMap,
711 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
712 configMap.entrySet().stream().filter(x -> !stateMap.containsKey(x.getKey())).forEach(
713 y -> builder.addChild(y.getValue()));
714 stateMap.entrySet().stream().filter(x -> !configMap.containsKey(x.getKey())).forEach(
715 y -> builder.addChild(y.getValue()));
719 * Map data with the same identifiers to builder. Data with the same identifiers cannot be just added but we need to
720 * go one level down with {@code prepareData} method.
723 * immutable config data
725 * immutable state data
729 @SuppressWarnings("unchecked")
730 private static <T extends NormalizedNode<? extends PathArgument, ?>> void mergeDataToBuilder(
731 @Nonnull final Map<PathArgument, T> configMap,
732 @Nonnull final Map<PathArgument, T> stateMap,
733 @Nonnull final NormalizedNodeContainerBuilder<?, PathArgument, T, ?> builder) {
734 // it is enough to process only config data because operational contains the same data
735 configMap.entrySet().stream().filter(x -> stateMap.containsKey(x.getKey())).forEach(
736 y -> builder.addChild((T) prepareData(y.getValue(), stateMap.get(y.getKey()))));