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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
32 * Base strategy for applying changes to a ContainerNode, irrespective of its
35 * @param <T> Type of the container node
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>() {
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);
49 final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
50 checkArgument(child != null, "Schema %s does not have a node for child %s", schema,
52 return SchemaAwareApplyOperation.from(child, treeConfig);
55 private final T schema;
56 private final DataTreeConfiguration treeConfig;
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");
65 protected final T getSchema() {
70 public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
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();
81 @SuppressWarnings("rawtypes")
82 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
85 public String toString() {
86 return getClass().getSimpleName() + " [" + schema + "]";