8b65d4628919631e16295ef8031b8c3eca8aec11
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodes.java
1 /*
2  * Copyright (c) 2015 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;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.collect.Iterables;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Optional;
17 import javax.xml.transform.dom.DOMSource;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
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.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40
41 /**
42  * Base strategy for converting an instance identifier into a normalized node structure.
43  * Use provided static methods for generic YangInstanceIdentifier -> NormalizedNode translation in ImmutableNodes.
44  */
45 abstract class InstanceIdToNodes<T extends PathArgument> implements Identifiable<T> {
46     private final T identifier;
47
48     InstanceIdToNodes(final T identifier) {
49         this.identifier = identifier;
50     }
51
52     @Override
53     public final T getIdentifier() {
54         return identifier;
55     }
56
57     /**
58      * Build a strategy for the next path argument.
59      *
60      * @param child child identifier
61      * @return transformation strategy for a specific child
62      */
63     abstract InstanceIdToNodes<?> getChild(PathArgument child);
64
65     /**
66      * Convert instance identifier into a NormalizedNode structure.
67      *
68      * @param instanceId Instance identifier to transform into NormalizedNodes
69      * @param deepestChild Optional normalized node to be inserted as the last child
70      * @param operation Optional modify operation to be set on the last child
71      * @return NormalizedNode structure corresponding to submitted instance ID
72      */
73     abstract @NonNull NormalizedNode<?, ?> create(PathArgument first, Iterator<PathArgument> others,
74             Optional<NormalizedNode<?, ?>> deepestChild);
75
76     abstract boolean isMixin();
77
78     private static final class UnkeyedListMixinNormalization extends InstanceIdToCompositeNodes<NodeIdentifier> {
79         private final UnkeyedListItemNormalization innerNode;
80
81         UnkeyedListMixinNormalization(final ListSchemaNode list) {
82             super(NodeIdentifier.create(list.getQName()));
83             this.innerNode = new UnkeyedListItemNormalization(list);
84         }
85
86         @Override
87         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> createBuilder(final PathArgument compositeNode) {
88             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
89         }
90
91         @Override
92         InstanceIdToNodes<?> getChild(final PathArgument child) {
93             return child.getNodeType().equals(getIdentifier().getNodeType()) ? innerNode : null;
94         }
95
96         @Override
97         boolean isMixin() {
98             return true;
99         }
100     }
101
102     private static final class AnyXmlNormalization extends InstanceIdToNodes<NodeIdentifier> {
103         AnyXmlNormalization(final AnyXmlSchemaNode schema) {
104             super(NodeIdentifier.create(schema.getQName()));
105         }
106
107         @Override
108         InstanceIdToNodes<?> getChild(final PathArgument child) {
109             return null;
110         }
111
112         @Override
113         NormalizedNode<?, ?> create(final PathArgument first, final Iterator<PathArgument> others,
114                 final Optional<NormalizedNode<?, ?>> deepestChild) {
115             final NormalizedNodeBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder = Builders.anyXmlBuilder()
116                     .withNodeIdentifier(getIdentifier());
117             if (deepestChild.isPresent()) {
118                 final NormalizedNode<?, ?> child = deepestChild.get();
119                 checkState(child instanceof AnyXmlNode);
120                 builder.withValue(((AnyXmlNode) child).getValue());
121             }
122
123             return builder.build();
124         }
125
126         @Override
127         boolean isMixin() {
128             return false;
129         }
130     }
131
132     private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
133         DataSchemaNode potential = parent.getDataChildByName(child);
134         if (potential == null) {
135             potential = findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child);
136         }
137         return Optional.ofNullable(potential);
138     }
139
140     static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
141         final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
142         checkArgument(potential.isPresent(),
143                 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
144                 schema.getChildNodes());
145
146         final DataSchemaNode result = potential.get();
147         // We try to look up if this node was added by augmentation
148         if (schema instanceof DataSchemaNode && result.isAugmenting()) {
149             return fromAugmentation(schema, (AugmentationTarget) schema, result);
150         }
151         return fromDataSchemaNode(result);
152     }
153
154     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
155         for (final ChoiceSchemaNode choice : choices) {
156             for (final CaseSchemaNode caze : choice.getCases().values()) {
157                 if (findChildSchemaNode(caze, child).isPresent()) {
158                     return choice;
159                 }
160             }
161         }
162         return null;
163     }
164
165     /**
166      * Returns a SchemaPathUtil for provided child node
167      * <p/>
168      * If supplied child is added by Augmentation this operation returns
169      * a SchemaPathUtil for augmentation,
170      * otherwise returns a SchemaPathUtil for child as
171      * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
172      */
173     private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
174             final AugmentationTarget parentAug, final DataSchemaNode child) {
175         for (final AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
176             final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
177             if (potential != null) {
178                 return new InstanceIdToCompositeNodes.AugmentationNormalization(aug, parent);
179             }
180         }
181         return fromDataSchemaNode(child);
182     }
183
184     static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
185         if (potential instanceof ContainerSchemaNode) {
186             return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
187         } else if (potential instanceof ListSchemaNode) {
188             return fromListSchemaNode((ListSchemaNode) potential);
189         } else if (potential instanceof LeafSchemaNode) {
190             return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
191         } else if (potential instanceof ChoiceSchemaNode) {
192             return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
193         } else if (potential instanceof LeafListSchemaNode) {
194             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
195         } else if (potential instanceof AnyXmlSchemaNode) {
196             return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
197         }
198         return null;
199     }
200
201     private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
202         final List<QName> keyDefinition = potential.getKeyDefinition();
203         if (keyDefinition == null || keyDefinition.isEmpty()) {
204             return new UnkeyedListMixinNormalization(potential);
205         }
206         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential)
207                 : new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
208     }
209
210     private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
211         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential)
212                 : new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);
213     }
214 }