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.util;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.Iterables;
12 import java.util.List;
13 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
26 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
39 * Schema derived data providing necessary information for mapping between
40 * {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode} and serialization format defined in RFC6020,
41 * since the mapping is not one-to-one.
43 * @param <T> Path Argument type
45 public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
46 private final DataSchemaNode dataSchemaNode;
47 private final T identifier;
49 protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
50 this.identifier = identifier;
51 if (schema instanceof DataSchemaNode) {
52 this.dataSchemaNode = (DataSchemaNode) schema;
54 this.dataSchemaNode = null;
59 public T getIdentifier() {
63 public boolean isMixin() {
67 public boolean isKeyedEntry() {
71 public abstract boolean isLeaf();
73 protected Set<QName> getQNameIdentifiers() {
74 return ImmutableSet.of(identifier.getNodeType());
78 * Find a child node identifier by its {@link PathArgument}.
80 * @param child Child path argument
81 * @return A child node, or null if not found
83 public abstract @Nullable DataSchemaContextNode<?> getChild(PathArgument child);
85 public abstract @Nullable DataSchemaContextNode<?> getChild(QName child);
87 public @Nullable DataSchemaNode getDataSchemaNode() {
88 return dataSchemaNode;
92 * Find a child node as identified by a {@link YangInstanceIdentifier} relative to this node.
94 * @param path Path towards the child node
95 * @return Child node if present, or empty when corresponding child is not found.
96 * @throws NullPointerException if {@code path} is null
98 public final @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(
99 final @NonNull YangInstanceIdentifier path) {
100 DataSchemaContextNode<?> currentOp = this;
101 for (PathArgument arg : path.getPathArguments()) {
102 currentOp = currentOp.getChild(arg);
103 if (currentOp == null) {
104 return Optional.empty();
107 return Optional.of(currentOp);
110 static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
111 final DataSchemaNode potential = parent.getDataChildByName(child);
112 return potential == null ? findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child)
116 static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
117 final DataSchemaNode result = findChildSchemaNode(schema, child);
118 // We try to look up if this node was added by augmentation
119 if (result != null && schema instanceof DataSchemaNode && result.isAugmenting()) {
120 return fromAugmentation(schema, (AugmentationTarget) schema, result);
122 return fromDataSchemaNode(result);
125 // FIXME: this looks like it should be a Predicate on a stream with findFirst()
126 private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
127 for (ChoiceSchemaNode choice : choices) {
128 // FIXME: this looks weird: what are we looking for again?
129 for (CaseSchemaNode caze : choice.getCases().values()) {
130 if (findChildSchemaNode(caze, child) != null) {
139 * Create AugmentationIdentifier from an AugmentationSchemaNode.
141 * @param schema Augmentation schema
142 * @return AugmentationIdentifier for the schema
143 * @throws NullPointerException if {@code schema} is null
145 public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode schema) {
146 return new AugmentationIdentifier(schema.getChildNodes().stream().map(DataSchemaNode::getQName)
147 .collect(Collectors.toSet()));
151 * Returns an AugmentationSchemaNode as effective in a parent node.
153 * @param schema Augmentation schema
154 * @param parent Parent schema
155 * @return Adjusted Augmentation schema
156 * @throws NullPointerException if any of the arguments is null
157 * @deprecated Use {@link EffectiveAugmentationSchema#create(AugmentationSchemaNode, DataNodeContainer)} instead.
160 public static AugmentationSchemaNode augmentationProxy(final AugmentationSchemaNode schema,
161 final DataNodeContainer parent) {
162 return EffectiveAugmentationSchema.create(schema, parent);
166 * Returns a DataContextNodeOperation for provided child node
169 * If supplied child is added by Augmentation this operation returns a
170 * DataContextNodeOperation for augmentation, otherwise returns a
171 * DataContextNodeOperation for child as call for
172 * {@link #fromDataSchemaNode(DataSchemaNode)}.
174 static @Nullable DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
175 final AugmentationTarget parentAug, final DataSchemaNode child) {
176 for (AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
177 if (aug.findDataChildByName(child.getQName()).isPresent()) {
178 return new AugmentationContextNode(aug, parent);
181 return fromDataSchemaNode(child);
184 public static @Nullable DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
185 if (potential instanceof ContainerSchemaNode) {
186 return new ContainerContextNode((ContainerSchemaNode) potential);
187 } else if (potential instanceof ListSchemaNode) {
188 return fromListSchemaNode((ListSchemaNode) potential);
189 } else if (potential instanceof LeafSchemaNode) {
190 return new LeafContextNode((LeafSchemaNode) potential);
191 } else if (potential instanceof ChoiceSchemaNode) {
192 return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
193 } else if (potential instanceof LeafListSchemaNode) {
194 return fromLeafListSchemaNode((LeafListSchemaNode) potential);
195 } else if (potential instanceof AnyXmlSchemaNode) {
196 return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
201 private static DataSchemaContextNode<?> fromListSchemaNode(final ListSchemaNode potential) {
202 List<QName> keyDefinition = potential.getKeyDefinition();
203 if (keyDefinition == null || keyDefinition.isEmpty()) {
204 return new UnkeyedListMixinContextNode(potential);
206 if (potential.isUserOrdered()) {
207 return new OrderedMapMixinContextNode(potential);
209 return new UnorderedMapMixinContextNode(potential);
212 private static DataSchemaContextNode<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
213 if (potential.isUserOrdered()) {
214 return new OrderedLeafListMixinContextNode(potential);
216 return new UnorderedLeafListMixinContextNode(potential);
219 public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
220 return new ContainerContextNode(ctx);