Cleanup use of Guava library
[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.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.util.concurrent.UncheckedExecutionException;
15 import java.util.Optional;
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>
36         extends AbstractNodeContainerModificationStrategy {
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataNodeContainerModificationStrategy.class);
38     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
39             .build(new CacheLoader<PathArgument, ModificationApplyOperation>() {
40                 @Override
41                 public ModificationApplyOperation load(@Nonnull final PathArgument key) {
42                     if (key instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
43                         return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema,
44                             (AugmentationIdentifier) key, treeConfig);
45                     }
46
47                     final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
48                     Preconditions.checkArgument(child != null, "Schema %s does not have a node for child %s", schema,
49                             key.getNodeType());
50                     return SchemaAwareApplyOperation.from(child, treeConfig);
51                 }
52             });
53     private final T schema;
54     private final DataTreeConfiguration treeConfig;
55
56     protected AbstractDataNodeContainerModificationStrategy(final T schema,
57             final Class<? extends NormalizedNode<?, ?>> nodeClass, final DataTreeConfiguration treeConfig) {
58         super(nodeClass, treeConfig);
59         this.schema = Preconditions.checkNotNull(schema,"schema");
60         this.treeConfig = Preconditions.checkNotNull(treeConfig,"treeConfig");
61     }
62
63     protected final T getSchema() {
64         return schema;
65     }
66
67     @Override
68     public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
69         try {
70             return Optional.ofNullable(childCache.get(identifier));
71         } catch (ExecutionException | UncheckedExecutionException e) {
72             LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
73                 schema.getChildNodes(), e.getCause());
74             return Optional.empty();
75         }
76     }
77
78     @Override
79     @SuppressWarnings("rawtypes")
80     protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
81
82     @Override
83     public String toString() {
84         return getClass().getSimpleName() + " [" + schema + "]";
85     }
86 }