2 * Copyright (c) 2013 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 com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashSet;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
31 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 public final class SchemaUtils {
37 private static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
39 public QName apply(@Nonnull final DataSchemaNode input) {
40 return input.getQName();
44 private SchemaUtils() {
45 throw new UnsupportedOperationException();
49 * @param qname - schema node to find
50 * @param dataSchemaNode - iterable of schemaNodes to look through
51 * @return - schema node with newest revision or absent if no schema node with matching qname is found
53 public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
54 DataSchemaNode sNode = null;
55 if (dataSchemaNode != null && qname != null) {
56 for (DataSchemaNode dsn : dataSchemaNode) {
57 if (qname.isEqualWithoutRevision(dsn.getQName())) {
58 if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
61 } else if (dsn instanceof ChoiceSchemaNode) {
62 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
64 final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
65 if (dataChildByName != null) {
66 return Optional.of(dataChildByName);
68 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
69 if (foundDsn.isPresent()) {
76 return Optional.fromNullable(sNode);
81 * Find child schema node identified by its QName within a provided schema node
83 * @param schema schema for parent node - search root
84 * @param qname qname(with or without a revision) of a child node to be found in the parent schema
85 * @return found schema node
86 * @throws java.lang.IllegalStateException if the child was not found in parent schema node
88 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
89 // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices
90 final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
91 return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
95 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) {
97 return findSchemaForChild(schema, qname);
100 Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
101 if (!childSchemaOptional.isPresent()) {
104 return childSchemaOptional.get();
107 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
108 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
109 Preconditions.checkState(childSchema.isPresent(),
110 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
111 return childSchema.get();
114 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
115 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
116 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
118 return schemaForAugment.get();
121 public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
122 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
124 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
125 schemaForAugment = findAugment(choiceCaseNode, qNames);
126 if(schemaForAugment.isPresent()) {
131 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
133 return schemaForAugment.get();
136 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
137 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
139 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
141 public QName apply(@Nonnull final DataSchemaNode input) {
142 Preconditions.checkNotNull(input);
143 return input.getQName();
147 if(qNamesFromAugment.equals(qNames)) {
148 return Optional.of(augment);
152 return Optional.absent();
155 public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
156 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
157 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
158 if (childSchema.isPresent()) {
159 return childSchema.get();
164 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
165 childPartialQName, schema));
169 * Recursively find all child nodes that come from choices.
171 * @return Map with all child nodes, to their most top augmentation
173 public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
174 return mapChildElementsFromChoices(schema, schema.getChildNodes());
177 private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
178 Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
180 for (final DataSchemaNode childSchema : childNodes) {
181 if (childSchema instanceof ChoiceSchemaNode) {
183 if (isFromAugment(schema, childSchema)) {
187 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
189 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
190 mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
196 return mappedChoices;
199 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
200 if (!(schema instanceof AugmentationTarget)) {
204 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
205 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
214 * Recursively find all child nodes that come from augmentations.
216 * @return Map with all child nodes, to their most top augmentation
218 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
220 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
222 // Find QNames of augmented child nodes
223 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
224 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
225 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
226 augments.put(dataSchemaNode.getQName(), augmentationSchema);
230 // Augmented nodes have to be looked up directly in augmentationTarget
231 // because nodes from augment do not contain nodes from other augmentations
232 if (schema instanceof DataNodeContainer) {
234 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
235 // If is not augmented child, continue
236 if (!(augments.containsKey(child.getQName()))) {
240 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
242 // recursively add all child nodes in case of augment, case and choice
243 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
244 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
245 childNodesToAugmentation.put(qName, mostTopAugmentation);
247 } else if (child instanceof ChoiceSchemaNode) {
248 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
249 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
250 childNodesToAugmentation.put(qName, mostTopAugmentation);
254 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
259 // Choice Node has to map child nodes from all its cases
260 if (schema instanceof ChoiceSchemaNode) {
261 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
262 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
266 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
267 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
272 return childNodesToAugmentation;
276 * Recursively list all child nodes.
278 * In case of choice, augment and cases, step in.
280 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
281 Set<QName> allChildNodes = Sets.newHashSet();
283 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
284 if(childSchema instanceof ChoiceSchemaNode) {
285 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
286 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
288 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
289 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
292 allChildNodes.add(childSchema.getQName());
296 return allChildNodes;
300 * Retrieves real schemas for augmented child node.
302 * Schema of the same child node from augment, and directly from target is not the same.
303 * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
306 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
307 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
308 return Collections.emptySet();
311 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
313 if(targetSchema instanceof DataNodeContainer) {
314 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
315 } else if(targetSchema instanceof ChoiceSchemaNode) {
316 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
317 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
318 if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
319 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
325 return realChildNodes;
328 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
329 final AugmentationSchema augmentSchema) {
330 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
331 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
332 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
333 realChildNodes.add(realChild);
335 return realChildNodes;
338 public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
339 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
340 if (child instanceof AugmentationNode
341 && belongsToCaseAugment(choiceCaseNode,
342 (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
343 return Optional.of(choiceCaseNode);
344 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
345 return Optional.of(choiceCaseNode);
349 return Optional.absent();
352 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
353 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
355 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
356 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
357 currentAugmentChildNodes.add(dataSchemaNode.getQName());
360 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
369 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
370 * node is found then it is returned, else null.
372 public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
373 if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
374 for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
375 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
376 if (childInAugmentation != null) {
384 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
385 final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
386 return new YangInstanceIdentifier.AugmentationIdentifier(ImmutableSet.copyOf(qnames));