Introduce top-level pom file.
[mdsal.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.binding.generator.util;
9
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;
13 import java.net.URI;
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;
28
29 public final class YangSchemaUtils {
30     public static final String AUGMENT_IDENTIFIER = "augment-identifier";
31
32     private YangSchemaUtils() {
33         throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
34     }
35
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) {
40             return identifier;
41         }
42         URI namespace = null;
43         Date revision = null;
44         if(augmentation instanceof NamespaceRevisionAware) {
45             namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
46             revision = ((NamespaceRevisionAware) augmentation).getRevision();
47         }
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();
54                     break;
55                 }
56             }
57         }
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");
62     }
63
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();
68             }
69         }
70         return null;
71     }
72
73     @Nullable
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.");
77
78         QName currentArg = arguments.next();
79         DataNodeContainer currentNode = context.findModuleByNamespaceAndRevision(currentArg.getNamespace(), currentArg.getRevision());
80         if(currentNode == null) {
81             return null;
82         }
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);
93             } else {
94                 // Search in grouping
95                 for( final GroupingDefinition grouping : currentNode.getGroupings()) {
96                     if(currentArg.equals(grouping.getQName())) {
97                         currentNode = grouping;
98                         break;
99                     }
100                 }
101             }
102             currentArg = arguments.next();
103         }
104
105         for(final TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
106             if(typedef.getQName().equals(currentArg)) {
107                 return typedef;
108             }
109         }
110         return null;
111     }
112 }