2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. 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.yangtools.yang.data.impl.schema.tree;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.tree.NormalizedNodeContainerSupport.MapEntry;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 final class MapModificationStrategy extends AbstractNodeContainerModificationStrategy {
29 private static final MapEntry<OrderedMapNode> ORDERED_SUPPORT = new MapEntry<>(OrderedMapNode.class,
30 ChildTrackingPolicy.ORDERED, ImmutableOrderedMapNodeBuilder::create,
31 ImmutableOrderedMapNodeBuilder::create);
32 private static final MapEntry<MapNode> UNORDERED_SUPPORT = new MapEntry<>(MapNode.class,
33 ChildTrackingPolicy.UNORDERED, ImmutableMapNodeBuilder::create, ImmutableMapNodeBuilder::create);
35 private final Optional<ModificationApplyOperation> entryStrategy;
36 private final MapNode emptyNode;
38 private MapModificationStrategy(final MapEntry<?> support, final ListSchemaNode schema,
39 final DataTreeConfiguration treeConfig, final MapNode emptyNode) {
40 super(support, treeConfig);
41 this.emptyNode = requireNonNull(emptyNode);
42 entryStrategy = Optional.of(ListEntryModificationStrategy.of(schema, treeConfig));
45 static MapModificationStrategy of(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
46 final MapEntry<?> support;
47 final MapNode emptyNode;
48 if (schema.isUserOrdered()) {
49 support = ORDERED_SUPPORT;
50 emptyNode = ImmutableNodes.orderedMapNode(schema.getQName());
52 support = UNORDERED_SUPPORT;
53 emptyNode = ImmutableNodes.mapNode(schema.getQName());
55 return new MapModificationStrategy(support, schema, treeConfig, emptyNode);
58 // FIXME: this is a hack, originally introduced in
59 // Change-Id: I9dc02a1917f38e8a0d62279843974b9869c48693. DataTreeRoot needs to be fixed up to properly
60 // handle the lookup of through maps.
62 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
63 if (identifier instanceof NodeIdentifierWithPredicates) {
66 // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
67 // try to retrieve the child that the identifier should be pointing to from our entryStrategy
68 // if we have one. If the entryStrategy cannot find this child we just return the absent
70 return entryStrategy.get().getChild(identifier);
74 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
75 final Version version) {
76 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
81 void checkApplicable(final ModificationPath path, final NodeModification modification,
82 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
83 AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
88 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
89 return super.addToStringAttributes(helper).add("entry", entryStrategy.get());