Merge "BUG-2955: Fix error reporting for wrong YangInstanceIdentifier"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataNodeContainerModificationStrategy.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 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.util.concurrent.UncheckedExecutionException;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Base strategy for applying changes to a ContainerNode, irrespective of its
29  * actual type.
30  *
31  * @param <T> Type of the container node
32  */
33 abstract class AbstractDataNodeContainerModificationStrategy<T extends DataNodeContainer> extends AbstractNodeContainerModificationStrategy {
34     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataNodeContainerModificationStrategy.class);
35     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
36             .build(new CacheLoader<PathArgument, ModificationApplyOperation>() {
37                 @Override
38                 public ModificationApplyOperation load(final PathArgument key) {
39                     if (key instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
40                         return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) key);
41                     }
42
43                     final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
44                     Preconditions.checkArgument(child != null, "Schema %s does not have a node for child %s", schema, key.getNodeType());
45                     return SchemaAwareApplyOperation.from(child);
46                 }
47             });
48     private final T schema;
49
50     protected AbstractDataNodeContainerModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
51         super(nodeClass);
52         this.schema = Preconditions.checkNotNull(schema);
53     }
54
55     protected final T getSchema() {
56         return schema;
57     }
58
59     @Override
60     public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
61         try {
62             return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
63         } catch (ExecutionException | UncheckedExecutionException e) {
64             LOG.trace("Child {} not present in container schema {} children {}", identifier, this, schema.getChildNodes(), e.getCause());
65             return Optional.absent();
66         }
67     }
68
69     @Override
70     @SuppressWarnings("rawtypes")
71     protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
72
73     @Override
74     public String toString() {
75         return getClass().getSimpleName() + " [" + schema + "]";
76     }
77 }