Move SchemaUtils to yang-data-util
[yangtools.git] / yang / 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         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);
37             }
38         }
39
40         return Optional.empty();
41     }
42
43     private static boolean belongsToCaseAugment(final CaseSchemaNode caseNode,
44             final AugmentationIdentifier childToProcess) {
45         for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) {
46
47             final Set<QName> currentAugmentChildNodes = new HashSet<>();
48             for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
49                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
50             }
51
52             if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)) {
53                 return true;
54             }
55         }
56
57         return false;
58     }
59
60     /**
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.
63      *
64      * @param parent parent node
65      * @param child child node
66      * @return augmentation schema
67      */
68     public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
69             final DataSchemaNode child) {
70         if (!(parent instanceof AugmentationTarget) || parent instanceof ChoiceSchemaNode) {
71             return null;
72         }
73
74         for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
75             final Optional<DataSchemaNode> childInAugmentation = augmentation.findDataChildByName(child.getQName());
76             if (childInAugmentation.isPresent()) {
77                 return augmentation;
78             }
79         }
80         return null;
81     }
82 }