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