02ddfdadc5f1d379439b1f587cbec7868b33f3f1
[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.HashSet;
12 import java.util.Optional;
13 import java.util.Set;
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;
23
24 // FIXME: 8.0.0: re-examine usefulness of these methods
25 @Beta
26 public final class NormalizedNodeSchemaUtils {
27     private NormalizedNodeSchemaUtils() {
28         // Hidden on purpose
29     }
30
31     public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) {
32         if (child instanceof AugmentationNode) {
33             return detectCase(schema, (AugmentationNode) child);
34         }
35
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);
40             }
41         }
42         return Optional.empty();
43     }
44
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);
50             }
51         }
52         return Optional.empty();
53     }
54
55     private static boolean belongsToCaseAugment(final CaseSchemaNode caseNode,
56             final AugmentationIdentifier childToProcess) {
57         for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) {
58
59             final Set<QName> currentAugmentChildNodes = new HashSet<>();
60             for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
61                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
62             }
63
64             if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)) {
65                 return true;
66             }
67         }
68
69         return false;
70     }
71
72     /**
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.
75      *
76      * @param parent parent node
77      * @param child child node
78      * @return augmentation schema
79      */
80     public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
81             final DataSchemaNode child) {
82         if (!(parent instanceof AugmentationTarget) || parent instanceof ChoiceSchemaNode) {
83             return null;
84         }
85
86         for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
87             final Optional<DataSchemaNode> childInAugmentation = augmentation.findDataChildByName(child.getQName());
88             if (childInAugmentation.isPresent()) {
89                 return augmentation;
90             }
91         }
92         return null;
93     }
94 }