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