Merge "Fixed incorrect test location."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / ASTSchemaSource.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.yangtools.yang.parser.util;
8
9 import com.google.common.annotations.Beta;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13
14 import javax.annotation.Nonnull;
15
16 import org.antlr.v4.runtime.ParserRuleContext;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
19 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
20 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
21
22 /**
23  * Abstract Syntax Tree representation of a schema source. This representation
24  * is internal to the YANG parser implementation, as it relies on ANTLR types.
25  *
26  * Instances of this representation are used for caching purposes, as they
27  * are a natural intermediate step in YANG text processing pipeline: the text
28  * has been successfully parsed, so we know it is syntactically correct. It also
29  * passes basic semantic validation and we were able to extract dependency
30  * information.
31  */
32 @Beta
33 public final class ASTSchemaSource implements SchemaSourceRepresentation {
34     public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
35         @Override
36         public SourceIdentifier apply(final ASTSchemaSource input) {
37             return input.getIdentifier();
38         }
39     };
40     public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
41         @Override
42         public YangModelDependencyInfo apply(final ASTSchemaSource input) {
43             return input.getDependencyInformation();
44         }
45     };
46     public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
47         @Override
48         public ParserRuleContext apply(final ASTSchemaSource input) {
49             return input.getAST();
50         }
51     };
52
53     private final YangModelDependencyInfo depInfo;
54     private final ParserRuleContext tree;
55     private final SourceIdentifier id;
56     private final String text;
57
58     private ASTSchemaSource(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo, final @Nonnull String text) {
59         this.depInfo = Preconditions.checkNotNull(depInfo);
60         this.tree = Preconditions.checkNotNull(tree);
61         this.id = Preconditions.checkNotNull(id);
62         this.text = Preconditions.checkNotNull(text);
63     }
64
65     /**
66      * Create a new instance of AST representation for a abstract syntax tree,
67      * performing minimal semantic analysis to acquire dependency information.
68      *
69      * @param name YANG source name. Used only for error reporting.
70      * @param tree ANTLR abstract syntax tree
71      * @return A new representation instance.
72      * @throws YangSyntaxErrorException if we fail to extract dependency information.
73      */
74     public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree) throws YangSyntaxErrorException {
75         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
76         final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
77         return new ASTSchemaSource(id, tree, depInfo, null);
78     }
79
80     /**
81      * Create a new instance of AST representation for a abstract syntax tree,
82      * performing minimal semantic analysis to acquire dependency information.
83      *
84      * @param name YANG source name. Used only for error reporting.
85      * @param tree ANTLR abstract syntax tree
86      * @return A new representation instance.
87      * @throws YangSyntaxErrorException if we fail to extract dependency information.
88      *
89      * @deprecated Migration only, will be removed as soon as the migration is completed.
90      */
91     @Deprecated
92     public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
93         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
94         final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
95         return new ASTSchemaSource(id, tree, depInfo, text);
96     }
97
98
99     @Override
100     public SourceIdentifier getIdentifier() {
101         return id;
102     }
103
104     @Override
105     public Class<? extends SchemaSourceRepresentation> getType() {
106         return ASTSchemaSource.class;
107     }
108
109     /**
110      * Return the underlying abstract syntax tree.
111      *
112      * @return Underlying AST.
113      */
114     public @Nonnull ParserRuleContext getAST() {
115         return tree;
116     }
117
118     /**
119      * Return the dependency information as extracted from the AST.
120      *
121      * FIXME: this method should be extracted into a public interface in the
122      *        model.api.repo class, relying solely on model.api types.
123      *
124      * @return Dependency information.
125      */
126     public @Nonnull YangModelDependencyInfo getDependencyInformation() {
127         return depInfo;
128     }
129
130     /**
131      * Return the semantically-equivalent text YANG text source.
132      *
133      * @return YANG text source
134      * @deprecated Used for migration purposes. Users are advised to use the
135      *             schema repository to acquire the representation of their
136      *             choice. Will be removed as soon as the migration is completed.
137      */
138     @Deprecated
139     public @Nonnull String getYangText() {
140         return text;
141     }
142 }