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.base.MoreObjects.ToStringHelper;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import com.google.common.util.concurrent.UncheckedExecutionException;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Base strategy for applying changes to a ContainerNode, irrespective of its
34 * @param <T> Type of the container node
36 class DataNodeContainerModificationStrategy<T extends DataNodeContainer>
37 extends AbstractNodeContainerModificationStrategy {
38 private static final Logger LOG = LoggerFactory.getLogger(DataNodeContainerModificationStrategy.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);
56 private final DataTreeConfiguration treeConfig;
57 private final T schema;
59 DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
60 final DataTreeConfiguration treeConfig) {
61 super(support, treeConfig);
62 this.schema = requireNonNull(schema, "schema");
63 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
67 public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
69 return Optional.ofNullable(childCache.get(identifier));
70 } catch (ExecutionException | UncheckedExecutionException e) {
71 LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
72 schema.getChildNodes(), e);
73 return Optional.empty();
78 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
79 return super.addToStringAttributes(helper).add("schema", schema);