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.Maps;
15 import com.google.common.collect.Sets;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 public final class SchemaUtils {
36 private SchemaUtils() {
40 * @param qname - schema node to find
41 * @param dataSchemaNode - iterable of schemaNodes to look through
42 * @return - schema node with newest revision or absent if no schema node with matching qname is found
44 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
45 DataSchemaNode sNode = null;
46 if (dataSchemaNode != null && qname != null) {
47 for (DataSchemaNode dsn : dataSchemaNode) {
48 if (qname.isEqualWithoutRevision(dsn.getQName())) {
49 if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
52 } else if (dsn instanceof ChoiceSchemaNode) {
53 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
55 final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
56 if (dataChildByName != null) {
57 return Optional.of(dataChildByName);
59 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
60 if (foundDsn.isPresent()) {
67 return Optional.fromNullable(sNode);
72 * Find child schema node identified by its QName within a provided schema node
74 * @param schema schema for parent node - search root
75 * @param qname qname(with or without a revision) of a child node to be found in the parent schema
76 * @return found schema node
77 * @throws java.lang.IllegalStateException if the child was not found in parent schema node
79 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
80 // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices
81 final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
82 return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
86 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) {
88 return findSchemaForChild(schema, qname);
91 Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
92 if (!childSchemaOptional.isPresent()) {
95 return childSchemaOptional.get();
98 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
99 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
100 Preconditions.checkState(childSchema.isPresent(),
101 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
102 return childSchema.get();
105 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
106 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
107 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
109 return schemaForAugment.get();
112 public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
113 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
115 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
116 schemaForAugment = findAugment(choiceCaseNode, qNames);
117 if(schemaForAugment.isPresent()) {
122 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
124 return schemaForAugment.get();
127 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
128 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
130 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
132 public QName apply(final @Nonnull DataSchemaNode input) {
133 Preconditions.checkNotNull(input);
134 return input.getQName();
138 if(qNamesFromAugment.equals(qNames)) {
139 return Optional.of(augment);
143 return Optional.absent();
146 public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
147 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
148 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
149 if (childSchema.isPresent()) {
150 return childSchema.get();
155 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
156 childPartialQName, schema));
160 * Recursively find all child nodes that come from choices.
162 * @return Map with all child nodes, to their most top augmentation
164 public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
165 return mapChildElementsFromChoices(schema, schema.getChildNodes());
168 private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
169 Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
171 for (final DataSchemaNode childSchema : childNodes) {
172 if (childSchema instanceof ChoiceSchemaNode) {
174 if (isFromAugment(schema, childSchema)) {
178 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
180 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
181 mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
187 return mappedChoices;
190 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
191 if (!(schema instanceof AugmentationTarget)) {
195 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
196 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
205 * Recursively find all child nodes that come from augmentations.
207 * @return Map with all child nodes, to their most top augmentation
209 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
211 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
213 // Find QNames of augmented child nodes
214 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
215 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
216 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
217 augments.put(dataSchemaNode.getQName(), augmentationSchema);
221 // Augmented nodes have to be looked up directly in augmentationTarget
222 // because nodes from augment do not contain nodes from other augmentations
223 if (schema instanceof DataNodeContainer) {
225 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
226 // If is not augmented child, continue
227 if (!(augments.containsKey(child.getQName()))) {
231 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
233 // recursively add all child nodes in case of augment, case and choice
234 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
235 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
236 childNodesToAugmentation.put(qName, mostTopAugmentation);
238 } else if (child instanceof ChoiceSchemaNode) {
239 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
240 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
241 childNodesToAugmentation.put(qName, mostTopAugmentation);
245 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
250 // Choice Node has to map child nodes from all its cases
251 if (schema instanceof ChoiceSchemaNode) {
252 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
253 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
257 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
258 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
263 return childNodesToAugmentation;
267 * Recursively list all child nodes.
269 * In case of choice, augment and cases, step in.
271 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
272 Set<QName> allChildNodes = Sets.newHashSet();
274 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
275 if(childSchema instanceof ChoiceSchemaNode) {
276 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
277 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
279 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
280 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
283 allChildNodes.add(childSchema.getQName());
287 return allChildNodes;
291 * Retrieves real schemas for augmented child node.
293 * Schema of the same child node from augment, and directly from target is not the same.
294 * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
297 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
298 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
299 return Collections.emptySet();
302 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
304 if(targetSchema instanceof DataNodeContainer) {
305 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
306 } else if(targetSchema instanceof ChoiceSchemaNode) {
307 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
308 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
309 if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
310 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
316 return realChildNodes;
319 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
320 final AugmentationSchema augmentSchema) {
321 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
322 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
323 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
324 realChildNodes.add(realChild);
326 return realChildNodes;
329 public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
330 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
331 if (child instanceof AugmentationNode
332 && belongsToCaseAugment(choiceCaseNode,
333 (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
334 return Optional.of(choiceCaseNode);
335 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
336 return Optional.of(choiceCaseNode);
340 return Optional.absent();
343 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
344 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
346 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
347 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
348 currentAugmentChildNodes.add(dataSchemaNode.getQName());
351 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
359 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
360 return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
363 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
364 Set<QName> qnames = Sets.newHashSet();
366 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
367 qnames.add(dataSchemaNode.getQName());