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;
12 import java.util.HashSet;
14 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.AugmentationNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import com.google.common.base.Function;
37 import com.google.common.base.Optional;
38 import com.google.common.cache.CacheBuilder;
39 import com.google.common.cache.CacheLoader;
40 import com.google.common.cache.LoadingCache;
43 * Base strategy for applying changes to a ContainerNode, irrespective of its
46 * @param <T> Type of the container node
48 abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
50 private final T schema;
51 private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
52 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
55 public ModificationApplyOperation apply(final PathArgument identifier) {
56 if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
57 return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
60 DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
64 return SchemaAwareApplyOperation.from(child);
68 protected DataNodeContainerModificationStrategy(final T schema,
69 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
74 protected T getSchema() {
79 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
81 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
82 } catch (ExecutionException e) {
83 return Optional.absent();
88 @SuppressWarnings("rawtypes")
89 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
92 public String toString() {
93 return getClass().getSimpleName() + " [" + schema + "]";
96 public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
98 protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
99 super(createAugmentProxy(schema,resolved), AugmentationNode.class);
103 @SuppressWarnings("rawtypes")
104 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
105 checkArgument(original instanceof AugmentationNode);
106 return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
110 private static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
111 Set<DataSchemaNode> realChildSchemas = new HashSet<>();
112 for(DataSchemaNode augChild : schema.getChildNodes()) {
113 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
115 return new AugmentationSchemaProxy(schema, realChildSchemas);
119 public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
121 public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
122 super(schemaNode, ContainerNode.class);
126 @SuppressWarnings("rawtypes")
127 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
128 checkArgument(original instanceof ContainerNode);
129 return ImmutableContainerNodeBuilder.create((ContainerNode) original);
133 public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
135 protected ListEntryModificationStrategy(final ListSchemaNode schema) {
136 super(schema, MapEntryNode.class);
140 @SuppressWarnings("rawtypes")
141 protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
142 checkArgument(original instanceof MapEntryNode);
143 return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
147 public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
149 public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
150 super(schemaNode, UnkeyedListEntryNode.class);
154 @SuppressWarnings("rawtypes")
155 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
156 checkArgument(original instanceof UnkeyedListEntryNode);
157 return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);