2cfeeaa471b78c16c4d5947c35e31b0be7ec5505
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / MapModificationStrategy.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static java.util.Objects.requireNonNull;
11
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;
27
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);
34
35     private final Optional<ModificationApplyOperation> entryStrategy;
36     private final MapNode emptyNode;
37
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));
43     }
44
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());
51         } else {
52             support = UNORDERED_SUPPORT;
53             emptyNode = ImmutableNodes.mapNode(schema.getQName());
54         }
55         return new MapModificationStrategy(support, schema, treeConfig, emptyNode);
56     }
57
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.
61     @Override
62     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
63         if (identifier instanceof NodeIdentifierWithPredicates) {
64             return entryStrategy;
65         }
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
69         // we get from it.
70         return entryStrategy.get().getChild(identifier);
71     }
72
73     @Override
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,
77             version);
78     }
79
80     @Override
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,
84             version);
85     }
86
87     @Override
88     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
89         return super.addToStringAttributes(helper).add("entry", entryStrategy.get());
90     }
91 }