Reduce use of getChildByName()
[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         final Optional<DataSchemaNode> potential = parent.findDataChildByName(child);
134         return potential.isPresent() ? potential : Optional.ofNullable(
135             findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child));
136     }
137
138     static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
139         final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
140         checkArgument(potential.isPresent(),
141                 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
142                 schema.getChildNodes());
143
144         final DataSchemaNode result = potential.get();
145         // We try to look up if this node was added by augmentation
146         if (schema instanceof DataSchemaNode && result.isAugmenting()) {
147             return fromAugmentation(schema, (AugmentationTarget) schema, result);
148         }
149         return fromDataSchemaNode(result);
150     }
151
152     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
153         for (final ChoiceSchemaNode choice : choices) {
154             for (final CaseSchemaNode caze : choice.getCases().values()) {
155                 if (findChildSchemaNode(caze, child).isPresent()) {
156                     return choice;
157                 }
158             }
159         }
160         return null;
161     }
162
163     /**
164      * Returns a SchemaPathUtil for provided child node
165      * <p/>
166      * If supplied child is added by Augmentation this operation returns
167      * a SchemaPathUtil for augmentation,
168      * otherwise returns a SchemaPathUtil for child as
169      * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
170      */
171     private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
172             final AugmentationTarget parentAug, final DataSchemaNode child) {
173         for (final AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
174             final Optional<DataSchemaNode> potential = aug.findDataChildByName(child.getQName());
175             if (potential.isPresent()) {
176                 return new InstanceIdToCompositeNodes.AugmentationNormalization(aug, parent);
177             }
178         }
179         return fromDataSchemaNode(child);
180     }
181
182     static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
183         if (potential instanceof ContainerSchemaNode) {
184             return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
185         } else if (potential instanceof ListSchemaNode) {
186             return fromListSchemaNode((ListSchemaNode) potential);
187         } else if (potential instanceof LeafSchemaNode) {
188             return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
189         } else if (potential instanceof ChoiceSchemaNode) {
190             return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
191         } else if (potential instanceof LeafListSchemaNode) {
192             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
193         } else if (potential instanceof AnyXmlSchemaNode) {
194             return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
195         }
196         return null;
197     }
198
199     private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
200         final List<QName> keyDefinition = potential.getKeyDefinition();
201         if (keyDefinition == null || keyDefinition.isEmpty()) {
202             return new UnkeyedListMixinNormalization(potential);
203         }
204         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential)
205                 : new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
206     }
207
208     private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
209         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential)
210                 : new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);
211     }
212 }