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.yangtools.sal.binding.generator.util;
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.base.Preconditions;
14 import java.util.Date;
15 import java.util.Iterator;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 public final class YangSchemaUtils {
30 public static final String AUGMENT_IDENTIFIER = "augment-identifier";
32 private YangSchemaUtils() {
33 throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
36 public static QName getAugmentationQName(final AugmentationSchema augmentation) {
37 checkNotNull(augmentation, "Augmentation must not be null.");
38 final QName identifier = getAugmentationIdentifier(augmentation);
39 if(identifier != null) {
44 if(augmentation instanceof NamespaceRevisionAware) {
45 namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
46 revision = ((NamespaceRevisionAware) augmentation).getRevision();
48 if(namespace == null || revision == null) {
49 for(final DataSchemaNode child : augmentation.getChildNodes()) {
50 // Derive QName from child nodes
51 if(!child.isAugmenting()) {
52 namespace = child.getQName().getNamespace();
53 revision = child.getQName().getRevision();
58 checkState(namespace != null, "Augmentation namespace must not be null");
59 checkState(revision != null, "Augmentation revision must not be null");
60 // FIXME: Allways return a qname with module namespace.
61 return QName.create(namespace,revision, "foo_augment");
64 public static QName getAugmentationIdentifier(final AugmentationSchema augmentation) {
65 for(final UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
66 if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
67 return extension.getQName();
74 public static TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
75 final Iterator<QName> arguments = path.getPathFromRoot().iterator();
76 Preconditions.checkArgument(arguments.hasNext(), "Type Definition path must contain at least one element.");
78 QName currentArg = arguments.next();
79 DataNodeContainer currentNode = context.findModuleByNamespaceAndRevision(currentArg.getNamespace(), currentArg.getRevision());
80 if(currentNode == null) {
83 // Last argument is type definition, so we need to cycle until we hit last argument.
84 while(arguments.hasNext()) {
85 // Nested private type - we need to find container/grouping to which type belongs.
86 final DataSchemaNode child = currentNode.getDataChildByName(currentArg);
87 if(child instanceof DataNodeContainer) {
88 currentNode = (DataNodeContainer) child;
89 } else if (child instanceof ChoiceSchemaNode) {
90 final QName caseQName = arguments.next();
91 Preconditions.checkArgument(arguments.hasNext(), "Path must not refer case only.");
92 currentNode = ((ChoiceSchemaNode) child).getCaseNodeByName(caseQName);
95 for( final GroupingDefinition grouping : currentNode.getGroupings()) {
96 if(currentArg.equals(grouping.getQName())) {
97 currentNode = grouping;
102 currentArg = arguments.next();
105 for(final TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
106 if(typedef.getQName().equals(currentArg)) {