2 * Copyright (c) 2015 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;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
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.
43 abstract class InstanceIdToNodes<T extends PathArgument> extends AbstractSimpleIdentifiable<T> {
44 InstanceIdToNodes(final T identifier) {
49 * Build a strategy for the next path argument.
51 * @param child child identifier
52 * @return transformation strategy for a specific child
54 abstract @Nullable InstanceIdToNodes<?> getChild(PathArgument child);
57 * Convert instance identifier into a NormalizedNode structure.
59 * @param first First path argument
60 * @param others Subsequent path arguments
61 * @return NormalizedNode structure corresponding to submitted instance ID
63 abstract @NonNull NormalizedNode create(PathArgument first, Iterator<PathArgument> others);
65 abstract boolean isMixin();
67 private static final class UnkeyedListMixinNormalization extends InstanceIdToCompositeNodes<NodeIdentifier> {
68 private final UnkeyedListItemNormalization innerNode;
70 UnkeyedListMixinNormalization(final ListSchemaNode list) {
71 super(NodeIdentifier.create(list.getQName()));
72 innerNode = new UnkeyedListItemNormalization(list);
76 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> createBuilder(final PathArgument compositeNode) {
77 return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
81 InstanceIdToNodes<?> getChild(final PathArgument child) {
82 return child.getNodeType().equals(getIdentifier().getNodeType()) ? innerNode : null;
91 private static final class OpaqueNormalization extends InstanceIdToNodes<NodeIdentifier> {
92 private OpaqueNormalization(final QName qname) {
93 super(NodeIdentifier.create(qname));
96 OpaqueNormalization(final AnydataSchemaNode schema) {
97 this(schema.getQName());
100 OpaqueNormalization(final AnyxmlSchemaNode schema) {
101 this(schema.getQName());
105 InstanceIdToNodes<?> getChild(final PathArgument child) {
115 NormalizedNode create(final PathArgument first, final Iterator<PathArgument> others) {
116 throw new IllegalStateException("Cannot instantiate opaque node without a value");
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));
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());
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);
137 return fromDataSchemaNode(result);
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()) {
152 * Returns a SchemaPathUtil for provided child node
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)}.
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);
167 return fromDataSchemaNode(child);
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);
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);
194 return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential)
195 : new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
198 private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
199 return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential)
200 : new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);