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.util;
10 import com.google.common.annotations.Beta;
11 import java.util.HashSet;
12 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
20 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 // FIXME: 8.0.0: re-examine usefulness of these methods
26 public final class NormalizedNodeSchemaUtils {
27 private NormalizedNodeSchemaUtils() {
31 public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) {
32 if (child instanceof AugmentationNode augment) {
33 return detectCase(schema, augment);
36 final QName childId = child.getIdentifier().getNodeType();
37 for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
38 if (choiceCaseNode.dataChildByName(childId) != null) {
39 return Optional.of(choiceCaseNode);
42 return Optional.empty();
45 public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema, final AugmentationNode child) {
46 final AugmentationIdentifier childId = child.getIdentifier();
47 for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
48 if (belongsToCaseAugment(choiceCaseNode, childId)) {
49 return Optional.of(choiceCaseNode);
52 return Optional.empty();
55 private static boolean belongsToCaseAugment(final CaseSchemaNode caseNode,
56 final AugmentationIdentifier childToProcess) {
57 for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) {
59 final Set<QName> currentAugmentChildNodes = new HashSet<>();
60 for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
61 currentAugmentChildNodes.add(dataSchemaNode.getQName());
64 if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)) {
73 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
74 * node is found then it is returned, else null.
76 * @param parent parent node
77 * @param child child node
78 * @return augmentation schema
80 public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
81 final DataSchemaNode child) {
82 if (parent instanceof AugmentationTarget target && !(parent instanceof ChoiceSchemaNode)) {
83 for (final AugmentationSchemaNode augmentation : target.getAvailableAugmentations()) {
84 if (augmentation.dataChildByName(child.getQName()) != null) {