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.FluentIterable;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.List;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
37 * Schema derived data providing necessary information for mapping
38 * between {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode}
39 * and serialization format defined in RFC6020, since the mapping
42 * @param <T> Path Argument type
45 public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
47 private final T identifier;
48 private final DataSchemaNode dataSchemaNode;
51 public T getIdentifier() {
55 protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
57 this.identifier = identifier;
58 if (schema instanceof DataSchemaNode) {
59 this.dataSchemaNode = (DataSchemaNode) schema;
61 this.dataSchemaNode = null;
65 public boolean isMixin() {
69 public boolean isKeyedEntry() {
73 protected Set<QName> getQNameIdentifiers() {
74 return Collections.singleton(identifier.getNodeType());
77 @Nullable public abstract DataSchemaContextNode<?> getChild(PathArgument child);
79 @Nullable public abstract DataSchemaContextNode<?> getChild(QName child);
81 public abstract boolean isLeaf();
84 @Nullable public DataSchemaNode getDataSchemaNode() {
85 return dataSchemaNode;
88 static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
89 DataSchemaNode potential = parent.getDataChildByName(child);
90 if (potential == null) {
91 Iterable<ChoiceSchemaNode> choices = FluentIterable.from(
92 parent.getChildNodes()).filter(ChoiceSchemaNode.class);
93 potential = findChoice(choices, child);
98 static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
99 DataSchemaNode result = findChildSchemaNode(schema, child);
100 // We try to look up if this node was added by augmentation
101 if (result != null && (schema instanceof DataSchemaNode) && result.isAugmenting()) {
102 return fromAugmentation(schema, (AugmentationTarget) schema, result);
104 return fromDataSchemaNode(result);
107 private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
108 ChoiceSchemaNode foundChoice = null;
109 choiceLoop: for (ChoiceSchemaNode choice : choices) {
110 for (ChoiceCaseNode caze : choice.getCases()) {
111 if (findChildSchemaNode(caze, child) != null) {
112 foundChoice = choice;
120 public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
121 ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
122 for (DataSchemaNode child : augmentation.getChildNodes()) {
123 potentialChildren.add(child.getQName());
125 return new AugmentationIdentifier(potentialChildren.build());
128 static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation,
129 final DataNodeContainer schema) {
130 Set<DataSchemaNode> children = new HashSet<>();
131 for (DataSchemaNode augNode : augmentation.getChildNodes()) {
132 children.add(schema.getDataChildByName(augNode.getQName()));
134 return new EffectiveAugmentationSchema(augmentation, children);
138 * 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)}.
146 @Nullable static DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
147 final AugmentationTarget parentAug, final DataSchemaNode child) {
148 AugmentationSchema augmentation = null;
149 for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
150 DataSchemaNode potential = aug.getDataChildByName(child.getQName());
151 if (potential != null) {
156 if (augmentation != null) {
157 return new AugmentationContextNode(augmentation, parent);
159 return fromDataSchemaNode(child);
162 @Nullable public static DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
163 if (potential instanceof ContainerSchemaNode) {
164 return new ContainerContextNode((ContainerSchemaNode) potential);
165 } else if (potential instanceof ListSchemaNode) {
166 return fromListSchemaNode((ListSchemaNode) potential);
167 } else if (potential instanceof LeafSchemaNode) {
168 return new LeafContextNode((LeafSchemaNode) potential);
169 } else if (potential instanceof ChoiceSchemaNode) {
170 return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
171 } else if (potential instanceof LeafListSchemaNode) {
172 return fromLeafListSchemaNode((LeafListSchemaNode) potential);
173 } else if (potential instanceof AnyXmlSchemaNode) {
174 return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
179 private static DataSchemaContextNode<?> fromListSchemaNode(final ListSchemaNode potential) {
180 List<QName> keyDefinition = potential.getKeyDefinition();
181 if (keyDefinition == null || keyDefinition.isEmpty()) {
182 return new UnkeyedListMixinContextNode(potential);
184 if (potential.isUserOrdered()) {
185 return new OrderedMapMixinContextNode(potential);
187 return new UnorderedMapMixinContextNode(potential);
190 private static DataSchemaContextNode<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
191 if (potential.isUserOrdered()) {
192 return new OrderedLeafListMixinContextNode(potential);
194 return new UnorderedLeafListMixinContextNode(potential);
197 public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
198 return new ContainerContextNode(ctx);