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