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