8dfe89f54b960fb366471c2b4c013eea5f3bfbb3
[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, "
41                 + "offending node: " + original);
42     }
43
44     @Override
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();
51         }
52         throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, "
53                 + "offending node: " + original);
54     }
55
56     @Override
57     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
58         if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
59             return entryStrategy;
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
64             // we get from it.
65             return entryStrategy.get().getChild(identifier);
66         }
67         return Optional.absent();
68     }
69
70     @Override
71     public String toString() {
72         return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
73     }
74 }