Merge "BUG-576: fixed text file loading in test."
[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 final class YangSchemaUtils {
29     public static final String AUGMENT_IDENTIFIER = "augment-identifier";
30
31     private YangSchemaUtils() {
32         throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
33     }
34
35     public static QName getAugmentationQName(final AugmentationSchema augmentation) {
36         checkNotNull(augmentation, "Augmentation must not be null.");
37         QName identifier = getAugmentationIdentifier(augmentation);
38         if(identifier != null) {
39             return identifier;
40         }
41         URI namespace = null;
42         Date revision = null;
43         if(augmentation instanceof NamespaceRevisionAware) {
44             namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
45             revision = ((NamespaceRevisionAware) augmentation).getRevision();
46         }
47         if(namespace == null || revision == null) {
48             for(DataSchemaNode child : augmentation.getChildNodes()) {
49                 // Derive QName from child nodes
50                 if(!child.isAugmenting()) {
51                     namespace = child.getQName().getNamespace();
52                     revision = child.getQName().getRevision();
53                     break;
54                 }
55             }
56         }
57         checkState(namespace != null, "Augmentation namespace must not be null");
58         checkState(revision != null, "Augmentation revision must not be null");
59         // FIXME: Allways return a qname with module namespace.
60         return QName.create(namespace,revision, "foo_augment");
61     }
62
63     public static QName getAugmentationIdentifier(final AugmentationSchema augmentation) {
64         for(UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
65             if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
66                 return extension.getQName();
67             }
68         }
69         return null;
70     }
71
72     public static TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
73         List<QName> arguments = path.getPath();
74         QName first = arguments.get(0);
75         QName typeQName = arguments.get(arguments.size() -1);
76         DataNodeContainer previous = context.findModuleByNamespaceAndRevision(first.getNamespace(), first.getRevision());
77         if(previous == null) {
78             return null;
79         }
80         checkArgument(arguments.size() == 1);
81         for(TypeDefinition<?> typedef : previous.getTypeDefinitions()) {
82             if(typedef.getQName().equals(typeQName)) {
83                 return typedef;
84             }
85         }
86         return null;
87     }
88 }