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.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 com.google.common.util.concurrent.UncheckedExecutionException;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Base strategy for applying changes to a ContainerNode, irrespective of its
31 * @param <T> Type of the container node
33 abstract class AbstractDataNodeContainerModificationStrategy<T extends DataNodeContainer> extends AbstractNodeContainerModificationStrategy {
34 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataNodeContainerModificationStrategy.class);
35 private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
36 .build(new CacheLoader<PathArgument, ModificationApplyOperation>() {
38 public ModificationApplyOperation load(final PathArgument key) {
39 if (key instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
40 return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) key);
43 final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
44 Preconditions.checkArgument(child != null, "Schema %s does not have a node for child %s", schema, key.getNodeType());
45 return SchemaAwareApplyOperation.from(child);
48 private final T schema;
50 protected AbstractDataNodeContainerModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
52 this.schema = Preconditions.checkNotNull(schema);
55 protected final T getSchema() {
60 public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
62 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
63 } catch (ExecutionException | UncheckedExecutionException e) {
64 LOG.trace("Child {} not present in container schema {} children {}", identifier, this, schema.getChildNodes(), e.getCause());
65 return Optional.absent();
70 @SuppressWarnings("rawtypes")
71 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
74 public String toString() {
75 return getClass().getSimpleName() + " [" + schema + "]";