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 com.google.common.base.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, offending node: " + original);
44 protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
45 if (original instanceof MapNode) {
46 return ImmutableMapNodeBuilder.create().withNodeIdentifier(((MapNode) original).getIdentifier()).build();
47 } else if (original instanceof MapEntryNode) {
48 return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(((MapEntryNode) original).getIdentifier()).build();
50 throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, offending node: " + original);
54 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
55 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
57 } else if (entryStrategy.isPresent()) {
58 // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
59 // try to retrieve the child that the identifier should be pointing to from our entryStrategy
60 // if we have one. If the entryStrategy cannot find this child we just return the absent
62 return entryStrategy.get().getChild(identifier);
64 return Optional.absent();
68 public String toString() {
69 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";