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 com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.MoreObjects;
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.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 // FIXME: createBuilder(), createEmptyValue() and getChild() here are hacks, originally introduced in
25 // Change-Id: I9dc02a1917f38e8a0d62279843974b9869c48693. DataTreeRoot needs to be fixed up to properly
26 // handle the lookup of through maps.
27 abstract class AbstractMapModificationStrategy extends AbstractNodeContainerModificationStrategy {
28 final Optional<ModificationApplyOperation> entryStrategy;
30 AbstractMapModificationStrategy(final Class<? extends MapNode> nodeClass, final ListSchemaNode schema,
31 final DataTreeConfiguration treeConfig) {
32 super(nodeClass, treeConfig);
33 entryStrategy = Optional.of(ListEntryModificationStrategy.of(schema, treeConfig));
37 @SuppressWarnings("rawtypes")
38 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
39 return ImmutableMapEntryNodeBuilder.create(checkCast(original));
43 protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
44 return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(checkCast(original).getIdentifier()).build();
48 public final Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
49 if (identifier instanceof NodeIdentifierWithPredicates) {
52 // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
53 // try to retrieve the child that the identifier should be pointing to from our entryStrategy
54 // if we have one. If the entryStrategy cannot find this child we just return the absent
56 return entryStrategy.get().getChild(identifier);
60 public final String toString() {
61 return MoreObjects.toStringHelper(this).add("entry", entryStrategy.get()).toString();
64 private static MapEntryNode checkCast(final NormalizedNode<?, ?> original) {
65 checkArgument(original instanceof MapEntryNode,
66 "MapModification strategy can only handle MapNode or MapEntryNodes, offending node: %s", original);
67 return (MapEntryNode) original;