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;
11 import com.google.common.collect.FluentIterable;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
17 import javax.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.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
38 * Schema derived data providing necessary information for mapping
39 * between {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode}
40 * and serialization format defined in RFC6020, since the mapping
43 * @param <T> Path Argument type
46 public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
48 private final T identifier;
49 private final DataSchemaNode dataSchemaNode;
52 public T getIdentifier() {
56 protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
58 this.identifier = identifier;
59 if (schema instanceof DataSchemaNode) {
60 this.dataSchemaNode = (DataSchemaNode) schema;
62 this.dataSchemaNode = null;
66 public boolean isMixin() {
70 public boolean isKeyedEntry() {
74 protected Set<QName> getQNameIdentifiers() {
75 return Collections.singleton(identifier.getNodeType());
78 @Nullable public abstract DataSchemaContextNode<?> getChild(final PathArgument child);
80 @Nullable public abstract DataSchemaContextNode<?> getChild(QName child);
82 public abstract boolean isLeaf();
85 @Nullable public DataSchemaNode getDataSchemaNode() {
86 return dataSchemaNode;
89 static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
90 DataSchemaNode potential = parent.getDataChildByName(child);
91 if (potential == null) {
92 Iterable<ChoiceSchemaNode> choices = FluentIterable.from(
93 parent.getChildNodes()).filter(ChoiceSchemaNode.class);
94 potential = findChoice(choices, child);
99 static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
100 DataSchemaNode result = findChildSchemaNode(schema, child);
101 // We try to look up if this node was added by augmentation
102 if (result != null && (schema instanceof DataSchemaNode) && result.isAugmenting()) {
103 return fromAugmentation(schema, (AugmentationTarget) schema, result);
105 return fromDataSchemaNode(result);
108 private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
109 ChoiceSchemaNode foundChoice = null;
110 choiceLoop: for (ChoiceSchemaNode choice : choices) {
111 for (ChoiceCaseNode caze : choice.getCases()) {
112 if (findChildSchemaNode(caze, child) != null) {
113 foundChoice = choice;
121 public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
122 ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
123 for (DataSchemaNode child : augmentation.getChildNodes()) {
124 potentialChildren.add(child.getQName());
126 return new AugmentationIdentifier(potentialChildren.build());
129 static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation,
130 final DataNodeContainer schema) {
131 Set<DataSchemaNode> children = new HashSet<>();
132 for (DataSchemaNode augNode : augmentation.getChildNodes()) {
133 children.add(schema.getDataChildByName(augNode.getQName()));
135 return new EffectiveAugmentationSchema(augmentation, children);
139 * Returns a DataContextNodeOperation for provided child node
141 * If supplied child is added by Augmentation this operation returns a
142 * DataContextNodeOperation for augmentation, otherwise returns a
143 * DataContextNodeOperation for child as call for
144 * {@link #fromDataSchemaNode(DataSchemaNode)}.
152 @Nullable static DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
153 final AugmentationTarget parentAug, final DataSchemaNode child) {
154 AugmentationSchema augmentation = null;
155 for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
156 DataSchemaNode potential = aug.getDataChildByName(child.getQName());
157 if (potential != null) {
162 if (augmentation != null) {
163 return new AugmentationContextNode(augmentation, parent);
165 return fromDataSchemaNode(child);
168 @Nullable public static DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
169 if (potential instanceof ContainerSchemaNode) {
170 return new ContainerContextNode((ContainerSchemaNode) potential);
171 } else if (potential instanceof ListSchemaNode) {
172 return fromListSchemaNode((ListSchemaNode) potential);
173 } else if (potential instanceof LeafSchemaNode) {
174 return new LeafContextNode((LeafSchemaNode) potential);
175 } else if (potential instanceof ChoiceSchemaNode) {
176 return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
177 } else if (potential instanceof LeafListSchemaNode) {
178 return fromLeafListSchemaNode((LeafListSchemaNode) potential);
179 } else if (potential instanceof AnyXmlSchemaNode) {
180 return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
185 private static DataSchemaContextNode<?> fromListSchemaNode(final ListSchemaNode potential) {
186 List<QName> keyDefinition = potential.getKeyDefinition();
187 if (keyDefinition == null || keyDefinition.isEmpty()) {
188 return new UnkeyedListMixinContextNode(potential);
190 if (potential.isUserOrdered()) {
191 return new OrderedMapMixinContextNode(potential);
193 return new UnorderedMapMixinContextNode(potential);
196 private static DataSchemaContextNode<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
197 if (potential.isUserOrdered()) {
198 return new OrderedLeafListMixinContextNode(potential);
200 return new UnorderedLeafListMixinContextNode(potential);
203 public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
204 return new ContainerContextNode(ctx);