2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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;
30 * Base strategy for applying changes to a ContainerNode, irrespective of its
33 * @param <T> Type of the container node
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>() {
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);
47 final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
48 Preconditions.checkArgument(child != null, "Schema %s does not have a node for child %s", schema,
50 return SchemaAwareApplyOperation.from(child, treeConfig);
53 private final T schema;
54 private final DataTreeConfiguration treeConfig;
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");
63 protected final T getSchema() {
68 public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
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();
79 @SuppressWarnings("rawtypes")
80 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
83 public String toString() {
84 return getClass().getSimpleName() + " [" + schema + "]";