Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / util / YangSchemaUtils.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.generator.util;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.base.Preconditions;
14 import java.net.URI;
15 import java.util.Date;
16 import java.util.Iterator;
17 import javax.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29
30 public final class YangSchemaUtils {
31     public static final String AUGMENT_IDENTIFIER = "augment-identifier";
32
33     private YangSchemaUtils() {
34         throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
35     }
36
37     public static QName getAugmentationQName(final AugmentationSchema augmentation) {
38         checkNotNull(augmentation, "Augmentation must not be null.");
39         final QName identifier = getAugmentationIdentifier(augmentation);
40         if(identifier != null) {
41             return identifier;
42         }
43         URI namespace = null;
44         Date revision = null;
45         if(augmentation instanceof NamespaceRevisionAware) {
46             namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
47             revision = ((NamespaceRevisionAware) augmentation).getRevision();
48         }
49         if(namespace == null || revision == null) {
50             for(final DataSchemaNode child : augmentation.getChildNodes()) {
51                 // Derive QName from child nodes
52                 if(!child.isAugmenting()) {
53                     namespace = child.getQName().getNamespace();
54                     revision = child.getQName().getRevision();
55                     break;
56                 }
57             }
58         }
59         checkState(namespace != null, "Augmentation namespace must not be null");
60         checkState(revision != null, "Augmentation revision must not be null");
61         // FIXME: Allways return a qname with module namespace.
62         return QName.create(namespace,revision, "foo_augment");
63     }
64
65     public static QName getAugmentationIdentifier(final AugmentationSchema augmentation) {
66         for(final UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
67             if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
68                 return extension.getQName();
69             }
70         }
71         return null;
72     }
73
74     @Nullable
75     public static TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
76         final Iterator<QName> arguments = path.getPathFromRoot().iterator();
77         Preconditions.checkArgument(arguments.hasNext(), "Type Definition path must contain at least one element.");
78
79         QName currentArg = arguments.next();
80         DataNodeContainer currentNode = context.findModuleByNamespaceAndRevision(currentArg.getNamespace(), currentArg.getRevision());
81         if(currentNode == null) {
82             return null;
83         }
84         // Last argument is type definition, so we need to cycle until we hit last argument.
85         while(arguments.hasNext()) {
86             // Nested private type - we need to find container/grouping to which type belongs.
87             final DataSchemaNode child = currentNode.getDataChildByName(currentArg);
88             if(child instanceof DataNodeContainer) {
89                 currentNode = (DataNodeContainer) child;
90             } else if (child instanceof ChoiceSchemaNode) {
91                 final QName caseQName = arguments.next();
92                 Preconditions.checkArgument(arguments.hasNext(), "Path must not refer case only.");
93                 currentNode = ((ChoiceSchemaNode) child).getCaseNodeByName(caseQName);
94             } else {
95                 // Search in grouping
96                 for (final GroupingDefinition grouping : currentNode.getGroupings()) {
97                     if (currentArg.equals(grouping.getQName())) {
98                         currentNode = grouping;
99                         break;
100                     }
101                 }
102             }
103             currentArg = arguments.next();
104         }
105
106         for(final TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
107             if(typedef.getQName().equals(currentArg)) {
108                 return typedef;
109             }
110         }
111         return null;
112     }
113 }