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 for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
33 if (child instanceof AugmentationNode
34 && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())
35 || choiceCaseNode.findDataChildByName(child.getNodeType()).isPresent()) {
36 return Optional.of(choiceCaseNode);
40 return Optional.empty();
43 private static boolean belongsToCaseAugment(final CaseSchemaNode caseNode,
44 final AugmentationIdentifier childToProcess) {
45 for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) {
47 final Set<QName> currentAugmentChildNodes = new HashSet<>();
48 for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
49 currentAugmentChildNodes.add(dataSchemaNode.getQName());
52 if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)) {
61 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
62 * node is found then it is returned, else null.
64 * @param parent parent node
65 * @param child child node
66 * @return augmentation schema
68 public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
69 final DataSchemaNode child) {
70 if (!(parent instanceof AugmentationTarget) || parent instanceof ChoiceSchemaNode) {
74 for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
75 final Optional<DataSchemaNode> childInAugmentation = augmentation.findDataChildByName(child.getQName());
76 if (childInAugmentation.isPresent()) {