Remove Augmentation{Identifier,Node}
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / NormalizedNodeSchemaUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import com.google.common.annotations.Beta;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
14 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
16 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19
20 // FIXME: 8.0.0: re-examine usefulness of these methods
21 @Beta
22 public final class NormalizedNodeSchemaUtils {
23     private NormalizedNodeSchemaUtils() {
24         // Hidden on purpose
25     }
26
27     public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) {
28         final QName childId = child.getIdentifier().getNodeType();
29         for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
30             if (choiceCaseNode.dataChildByName(childId) != null) {
31                 return Optional.of(choiceCaseNode);
32             }
33         }
34         return Optional.empty();
35     }
36
37     /**
38      * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
39      * node is found then it is returned, else null.
40      *
41      * @param parent parent node
42      * @param child child node
43      * @return augmentation schema
44      */
45     public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
46             final DataSchemaNode child) {
47         if (parent instanceof AugmentationTarget target && !(parent instanceof ChoiceSchemaNode)) {
48             for (final AugmentationSchemaNode augmentation : target.getAvailableAugmentations()) {
49                 if (augmentation.dataChildByName(child.getQName()) != null) {
50                     return augmentation;
51                 }
52             }
53         }
54         return null;
55     }
56 }