Fix list modification with DataTree rooted at MapEntryNode
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / UnorderedMapModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
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;
21
22 final class UnorderedMapModificationStrategy extends AbstractNodeContainerModificationStrategy {
23     private final Optional<ModificationApplyOperation> entryStrategy;
24
25     UnorderedMapModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
26         super(MapNode.class, treeConfig);
27         entryStrategy = Optional.of(new ListEntryModificationStrategy(schema, treeConfig));
28     }
29
30     @SuppressWarnings("rawtypes")
31     @Override
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);
39         }
40         throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, offending node: " + original);
41     }
42
43     @Override
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();
49         }
50         throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, offending node: " + original);
51     }
52
53     @Override
54     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
55         if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
56             return entryStrategy;
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
61             // we get from it.
62             return entryStrategy.get().getChild(identifier);
63         }
64         return Optional.absent();
65     }
66
67     @Override
68     public String toString() {
69         return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
70     }
71 }