2 * Copyright (c) 2014 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.mdsal.binding.generator.util;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
15 import java.util.Iterator;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 @Deprecated(forRemoval = true)
32 public final class YangSchemaUtils {
33 public static final String AUGMENT_IDENTIFIER = "augment-identifier";
35 private YangSchemaUtils() {
36 throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
39 public static QName getAugmentationQName(final AugmentationSchemaNode augmentation) {
40 requireNonNull(augmentation, "Augmentation must not be null.");
41 final QName identifier = getAugmentationIdentifier(augmentation);
42 if (identifier != null) {
46 Optional<Revision> revision = null;
47 if (augmentation instanceof NamespaceRevisionAware) {
48 namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
49 revision = ((NamespaceRevisionAware) augmentation).getRevision();
51 if (namespace == null || revision == null) {
52 for (DataSchemaNode child : augmentation.getChildNodes()) {
53 // Derive QName from child nodes
54 if (!child.isAugmenting()) {
55 namespace = child.getQName().getNamespace();
56 revision = child.getQName().getRevision();
61 checkState(namespace != null, "Augmentation namespace must not be null");
62 checkState(revision != null, "Augmentation revision must not be null");
63 // FIXME: Always return a qname with module namespace.
64 return QName.create(namespace, revision, "foo_augment");
67 public static QName getAugmentationIdentifier(final AugmentationSchemaNode augmentation) {
68 for (final UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
69 if (AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
70 return extension.getQName();
76 public static @Nullable TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
77 final Iterator<QName> arguments = path.getPathFromRoot().iterator();
78 Preconditions.checkArgument(arguments.hasNext(), "Type Definition path must contain at least one element.");
80 QName currentArg = arguments.next();
81 DataNodeContainer currentNode = context.findModule(currentArg.getModule()).orElse(null);
82 if (currentNode == null) {
85 // Last argument is type definition, so we need to cycle until we hit last argument.
86 while (arguments.hasNext()) {
87 // Nested private type - we need to find container/grouping to which type belongs.
88 final DataSchemaNode child = currentNode.getDataChildByName(currentArg);
89 if (child instanceof DataNodeContainer) {
90 currentNode = (DataNodeContainer) child;
91 } else if (child instanceof ChoiceSchemaNode) {
92 final QName caseQName = arguments.next();
93 Preconditions.checkArgument(arguments.hasNext(), "Path must not refer case only.");
94 currentNode = ((ChoiceSchemaNode) child).getCaseNodeByName(caseQName);
97 for (GroupingDefinition grouping : currentNode.getGroupings()) {
98 if (currentArg.equals(grouping.getQName())) {
99 currentNode = grouping;
104 currentArg = arguments.next();
107 for (TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
108 if (typedef.getQName().equals(currentArg)) {