Bug 466:Fixed namespace and revision resolution for empty augmentations.
[yangtools.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.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static com.google.common.base.Preconditions.checkState;
13
14 import java.net.URI;
15 import java.util.Date;
16 import java.util.List;
17
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.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27
28 public class YangSchemaUtils {
29
30     public static final String AUGMENT_IDENTIFIER = "augment-identifier";
31
32
33     public YangSchemaUtils() {
34         throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
35     }
36
37
38     public static QName getAugmentationQName(AugmentationSchema augmentation) {
39         checkNotNull(augmentation, "Augmentation must not be null.");
40         QName identifier = getAugmentationIdentifier(augmentation);
41         if(identifier != null) {
42             return identifier;
43         }
44         URI namespace = null;
45         Date revision = null;
46         if(augmentation instanceof NamespaceRevisionAware) {
47             namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
48             revision = ((NamespaceRevisionAware) augmentation).getRevision();
49         }
50         if(namespace == null || revision == null)
51         for(DataSchemaNode child : augmentation.getChildNodes()) {
52             // Derive QName from child nodes
53             if(!child.isAugmenting()) {
54                 namespace = child.getQName().getNamespace();
55                 revision = child.getQName().getRevision();
56                 break;
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(AugmentationSchema augmentation) {
66         for(UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
67             if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
68                 return extension.getQName();
69             }
70         }
71         return null;
72     }
73
74
75     public static TypeDefinition<?> findTypeDefinition(SchemaContext context, SchemaPath path) {
76         List<QName> arguments = path.getPath();
77         QName first = arguments.get(0);
78         QName typeQName = arguments.get(arguments.size() -1);
79         DataNodeContainer previous = context.findModuleByNamespaceAndRevision(first.getNamespace(), first.getRevision());
80         if(previous == null) {
81             return null;
82         }
83         checkArgument(arguments.size() == 1);
84         for (QName qName : arguments) {
85             //previous.getDataChildByName(qName);
86         }
87         for(TypeDefinition<?> typedef : previous.getTypeDefinitions()) {
88             if(typedef.getQName().equals(typeQName)) {
89                 return typedef;
90             }
91         }
92         return null;
93     }
94 }