Add utility wrappers for instantiating builders/nodes
[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.impl.schema.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.tree.NormalizedNodeContainerSupport.MapEntry;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24
25 final class MapModificationStrategy extends AbstractNodeContainerModificationStrategy {
26     private static final MapEntry<OrderedMapNode> ORDERED_SUPPORT = new MapEntry<>(OrderedMapNode.class,
27             ChildTrackingPolicy.ORDERED, ImmutableOrderedMapNodeBuilder::create,
28             ImmutableOrderedMapNodeBuilder::create);
29     private static final MapEntry<MapNode> UNORDERED_SUPPORT = new MapEntry<>(MapNode.class,
30             ChildTrackingPolicy.UNORDERED, ImmutableMapNodeBuilder::create, ImmutableMapNodeBuilder::create);
31
32     private final Optional<ModificationApplyOperation> entryStrategy;
33     private final MapNode emptyNode;
34
35     private MapModificationStrategy(final MapEntry<?> support, final ListSchemaNode schema,
36         final DataTreeConfiguration treeConfig, final MapNode emptyNode) {
37         super(support, treeConfig);
38         this.emptyNode = requireNonNull(emptyNode);
39         entryStrategy = Optional.of(ListEntryModificationStrategy.of(schema, treeConfig));
40     }
41
42     static MapModificationStrategy of(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
43         final MapEntry<?> support;
44         final MapNode emptyNode;
45         if (schema.isUserOrdered()) {
46             support = ORDERED_SUPPORT;
47             emptyNode = ImmutableNodes.orderedMapNode(schema.getQName());
48         } else {
49             support = UNORDERED_SUPPORT;
50             emptyNode = ImmutableNodes.mapNode(schema.getQName());
51         }
52         return new MapModificationStrategy(support, schema, treeConfig, emptyNode);
53     }
54
55     // FIXME: this is a hack, originally introduced in
56     //        Change-Id: I9dc02a1917f38e8a0d62279843974b9869c48693. DataTreeRoot needs to be fixed up to properly
57     //        handle the lookup of through maps.
58     @Override
59     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
60         if (identifier instanceof NodeIdentifierWithPredicates) {
61             return entryStrategy;
62         }
63         // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
64         // try to retrieve the child that the identifier should be pointing to from our entryStrategy
65         // if we have one. If the entryStrategy cannot find this child we just return the absent
66         // we get from it.
67         return entryStrategy.get().getChild(identifier);
68     }
69
70     @Override
71     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
72         return super.addToStringAttributes(helper).add("entry", entryStrategy.get());
73     }
74 }