Bug 2766: Fixed parsing and serializing XPath Instance Identifiers
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / DataNodeContainerModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Function;
13 import com.google.common.base.Optional;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.concurrent.ExecutionException;
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.AugmentationNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
39
40 /**
41  * Base strategy for applying changes to a ContainerNode, irrespective of its
42  * actual type.
43  *
44  * @param <T> Type of the container node
45  */
46 abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
47
48     private final T schema;
49     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
50             .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
51
52                 @Override
53                 public ModificationApplyOperation apply(final PathArgument identifier) {
54                     if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
55                         return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
56                     }
57
58                     DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
59                     if (child == null) {
60                         return null;
61                     }
62                     return SchemaAwareApplyOperation.from(child);
63                 }
64             }));
65
66     protected DataNodeContainerModificationStrategy(final T schema,
67             final Class<? extends NormalizedNode<?, ?>> nodeClass) {
68         super(nodeClass);
69         this.schema = schema;
70     }
71
72     protected T getSchema() {
73         return schema;
74     }
75
76     @Override
77     public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
78         try {
79             return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
80         } catch (ExecutionException e) {
81             return Optional.absent();
82         }
83     }
84
85     @Override
86     @SuppressWarnings("rawtypes")
87     protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
88
89     @Override
90     public String toString() {
91         return getClass().getSimpleName() + " [" + schema + "]";
92     }
93
94     public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
95
96         protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
97             super(createAugmentProxy(schema,resolved), AugmentationNode.class);
98         }
99
100         @Override
101         @SuppressWarnings("rawtypes")
102         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
103             checkArgument(original instanceof AugmentationNode);
104             return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
105         }
106
107         private static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
108             Set<DataSchemaNode> realChildSchemas = new HashSet<>();
109             for(DataSchemaNode augChild : schema.getChildNodes()) {
110                 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
111             }
112             return new EffectiveAugmentationSchema(schema, realChildSchemas);
113         }
114     }
115
116     public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
117
118         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
119             super(schemaNode, ContainerNode.class);
120         }
121
122         @Override
123         @SuppressWarnings("rawtypes")
124         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
125             checkArgument(original instanceof ContainerNode);
126             return ImmutableContainerNodeBuilder.create((ContainerNode) original);
127         }
128     }
129
130     public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
131
132         protected ListEntryModificationStrategy(final ListSchemaNode schema) {
133             super(schema, MapEntryNode.class);
134         }
135
136         @Override
137         @SuppressWarnings("rawtypes")
138         protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
139             checkArgument(original instanceof MapEntryNode);
140             return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
141         }
142     }
143
144     public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
145
146         public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
147             super(schemaNode, UnkeyedListEntryNode.class);
148         }
149
150         @Override
151         @SuppressWarnings("rawtypes")
152         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
153             checkArgument(original instanceof UnkeyedListEntryNode);
154             return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
155         }
156     }
157 }