2 * Copyright (c) 2014 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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 final class UnorderedMapModificationStrategy extends AbstractNodeContainerModificationStrategy {
23 private final Optional<ModificationApplyOperation> entryStrategy;
25 UnorderedMapModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
26 super(MapNode.class, treeConfig);
27 entryStrategy = Optional.of(new ListEntryModificationStrategy(schema, treeConfig));
30 @SuppressWarnings("rawtypes")
32 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
33 // If the DataTree is rooted at a MapEntryNode the original value will be MapEntryNode
34 // so make sure we can handle this aswell
35 if (original instanceof MapNode) {
36 return ImmutableMapNodeBuilder.create((MapNode) original);
37 } else if (original instanceof MapEntryNode) {
38 return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
40 throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, "
41 + "offending node: " + original);
45 protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
46 if (original instanceof MapNode) {
47 return ImmutableMapNodeBuilder.create().withNodeIdentifier(((MapNode) original).getIdentifier()).build();
48 } else if (original instanceof MapEntryNode) {
49 return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(
50 ((MapEntryNode) original).getIdentifier()).build();
52 throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, "
53 + "offending node: " + original);
57 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
58 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
60 } else if (entryStrategy.isPresent()) {
61 // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
62 // try to retrieve the child that the identifier should be pointing to from our entryStrategy
63 // if we have one. If the entryStrategy cannot find this child we just return the absent
65 return entryStrategy.get().getChild(identifier);
67 return Optional.empty();
71 public String toString() {
72 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";