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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Sets;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 public final class SchemaUtils {
40 private SchemaUtils() {
41 throw new UnsupportedOperationException();
45 * @param qname - schema node to find
46 * @param dataSchemaNode - iterable of schemaNodes to look through
47 * @return - schema node with newest revision or absent if no schema node with matching qname is found
49 public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
50 DataSchemaNode sNode = null;
51 if (dataSchemaNode != null && qname != null) {
52 for (DataSchemaNode dsn : dataSchemaNode) {
53 if (qname.isEqualWithoutRevision(dsn.getQName())) {
54 if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
57 } else if (dsn instanceof ChoiceSchemaNode) {
58 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
60 final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
61 if (dataChildByName != null) {
62 return Optional.of(dataChildByName);
64 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
65 if (foundDsn.isPresent()) {
72 return Optional.fromNullable(sNode);
77 * Find child schema node identified by its QName within a provided schema node
79 * @param schema schema for parent node - search root
80 * @param qname qname(with or without a revision) of a child node to be found in the parent schema
81 * @return found schema node
82 * @throws java.lang.IllegalStateException if the child was not found in parent schema node
84 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
85 // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices
86 final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
87 return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
91 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) {
93 return findSchemaForChild(schema, qname);
96 Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
97 if (!childSchemaOptional.isPresent()) {
100 return childSchemaOptional.get();
103 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
104 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
105 Preconditions.checkState(childSchema.isPresent(),
106 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
107 return childSchema.get();
110 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
111 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
112 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
114 return schemaForAugment.get();
117 public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
118 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
120 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
121 schemaForAugment = findAugment(choiceCaseNode, qNames);
122 if (schemaForAugment.isPresent()) {
127 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
129 return schemaForAugment.get();
132 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
133 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
134 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(),
135 DataSchemaNode::getQName));
137 if (qNamesFromAugment.equals(qNames)) {
138 return Optional.of(augment);
142 return Optional.absent();
145 public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
146 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
147 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
148 if (childSchema.isPresent()) {
149 return childSchema.get();
154 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
155 childPartialQName, schema));
159 * Recursively find all child nodes that come from choices.
161 * @param schema schema
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 * @param schema schema
208 * @return Map with all child nodes, to their most top augmentation
210 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
212 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
214 // Find QNames of augmented child nodes
215 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
216 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
217 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
218 augments.put(dataSchemaNode.getQName(), augmentationSchema);
222 // Augmented nodes have to be looked up directly in augmentationTarget
223 // because nodes from augment do not contain nodes from other augmentations
224 if (schema instanceof DataNodeContainer) {
226 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
227 // If is not augmented child, continue
228 if (!(augments.containsKey(child.getQName()))) {
232 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
234 // recursively add all child nodes in case of augment, case and choice
235 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
236 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
237 childNodesToAugmentation.put(qName, mostTopAugmentation);
239 } else if (child instanceof ChoiceSchemaNode) {
240 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
241 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
242 childNodesToAugmentation.put(qName, mostTopAugmentation);
246 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
251 // Choice Node has to map child nodes from all its cases
252 if (schema instanceof ChoiceSchemaNode) {
253 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
254 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
258 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
259 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
264 return childNodesToAugmentation;
268 * Recursively list all child nodes.
270 * In case of choice, augment and cases, step in.
272 * @param nodeContainer node container
273 * @return set of QNames
275 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
276 Set<QName> allChildNodes = Sets.newHashSet();
278 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
279 if (childSchema instanceof ChoiceSchemaNode) {
280 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
281 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
283 } else if (childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
284 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
287 allChildNodes.add(childSchema.getQName());
291 return allChildNodes;
295 * Retrieves real schemas for augmented child node.
297 * Schema of the same child node from augment, and directly from target is not the same.
298 * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
300 * @param targetSchema target schema
301 * @param augmentSchema augment schema
302 * @return set of nodes
304 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
305 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
306 return Collections.emptySet();
309 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
311 if (targetSchema instanceof DataNodeContainer) {
312 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
313 } else if (targetSchema instanceof ChoiceSchemaNode) {
314 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
315 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
316 if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
317 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
323 return realChildNodes;
326 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
327 final AugmentationSchema augmentSchema) {
328 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
329 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
330 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
331 realChildNodes.add(realChild);
333 return realChildNodes;
336 public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
337 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
338 if (child instanceof AugmentationNode
339 && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
340 return Optional.of(choiceCaseNode);
341 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
342 return Optional.of(choiceCaseNode);
346 return Optional.absent();
349 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final AugmentationIdentifier childToProcess) {
350 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
352 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
353 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
354 currentAugmentChildNodes.add(dataSchemaNode.getQName());
357 if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
366 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
367 * node is found then it is returned, else null.
369 * @param parent parent node
370 * @param child child node
371 * @return augmentation schema
373 public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
374 if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
375 for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
376 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
377 if (childInAugmentation != null) {
385 public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
386 final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), DataSchemaNode::getQName);
387 return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
391 * Finds schema node for given path in schema context.
392 * @param schemaContext schema context
394 * @return schema node on path
396 public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
397 SchemaNode current = Preconditions.checkNotNull(schemaContext);
398 for (final QName qname : path.getPathFromRoot()) {
399 current = findChildSchemaByQName(current, qname);
405 * Find child schema node identified by its QName within a provided schema node.
406 * @param node schema node
408 * @return child schema node
409 * @throws java.lang.IllegalArgumentException if the schema node does not allow children
411 public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) {
412 SchemaNode child = null;
414 if (node instanceof DataNodeContainer) {
415 child = ((DataNodeContainer) node).getDataChildByName(qname);
417 if (child == null && node instanceof SchemaContext) {
418 child = tryFindGroupings((SchemaContext) node, qname).orNull();
421 if (child == null && node instanceof SchemaContext) {
422 child = tryFindNotification((SchemaContext) node, qname)
423 .or(tryFindRpc(((SchemaContext) node), qname)).orNull();
425 } else if (node instanceof ChoiceSchemaNode) {
426 child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
427 } else if (node instanceof RpcDefinition) {
428 switch (qname.getLocalName()) {
430 child = ((RpcDefinition) node).getInput();
433 child = ((RpcDefinition) node).getOutput();
440 throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
446 private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
447 return Optional.fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
450 private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
451 return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
454 private static Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
455 return Optional.fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
458 private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
459 private final QName qname;
461 public SchemaNodePredicate(final QName qname) {
466 public boolean apply(final SchemaNode input) {
467 return input.getQName().equals(qname);