0af3e432c14154460d2ed7d339e053923c62db39
[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.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.antlr.v4.runtime.ParserRuleContext;
14 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
16 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
17 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
18
19 /**
20  * Abstract Syntax Tree representation of a schema source. This representation
21  * is internal to the YANG parser implementation, as it relies on ANTLR types.
22  *
23  * Instances of this representation are used for caching purposes, as they
24  * are a natural intermediate step in YANG text processing pipeline: the text
25  * has been successfully parsed, so we know it is syntactically correct. It also
26  * passes basic semantic validation and we were able to extract dependency
27  * information.
28  */
29 @Beta
30 public final class ASTSchemaSource implements SchemaSourceRepresentation {
31     public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
32         @Override
33         public SourceIdentifier apply(@Nonnull final ASTSchemaSource input) {
34             Preconditions.checkNotNull(input);
35             return input.getIdentifier();
36         }
37     };
38     public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
39         @Override
40         public YangModelDependencyInfo apply(@Nonnull final ASTSchemaSource input) {
41             Preconditions.checkNotNull(input);
42             return input.getDependencyInformation();
43         }
44     };
45     public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
46         @Override
47         public ParserRuleContext apply(@Nonnull final ASTSchemaSource input) {
48             Preconditions.checkNotNull(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(@Nonnull final SourceIdentifier id, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo, final String text) {
59         this.depInfo = Preconditions.checkNotNull(depInfo);
60         this.tree = Preconditions.checkNotNull(tree);
61         this.id = Preconditions.checkNotNull(id);
62         this.text = 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 ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
75         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
76         final SourceIdentifier id = getSourceId(depInfo);
77         return new ASTSchemaSource(id, tree, depInfo, null);
78     }
79
80     private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
81         final String name = depInfo.getName();
82         return depInfo.getFormattedRevision() == null
83                 ? new SourceIdentifier(name)
84                 : new SourceIdentifier(name, depInfo.getFormattedRevision());
85     }
86
87     /**
88      * Create a new instance of AST representation for a abstract syntax tree,
89      * performing minimal semantic analysis to acquire dependency information.
90      *
91      * @param name YANG source name. Used only for error reporting.
92      * @param tree ANTLR abstract syntax tree
93      * @return A new representation instance.
94      * @throws YangSyntaxErrorException if we fail to extract dependency information.
95      *
96      * @deprecated Migration only, will be removed as soon as the migration is completed.
97      */
98     @Deprecated
99     public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
100         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
101         final SourceIdentifier id = getSourceId(depInfo);
102         return new ASTSchemaSource(id, tree, depInfo, text);
103     }
104
105
106     @Override
107     public SourceIdentifier getIdentifier() {
108         return id;
109     }
110
111     @Override
112     public Class<? extends SchemaSourceRepresentation> getType() {
113         return ASTSchemaSource.class;
114     }
115
116     /**
117      * Return the underlying abstract syntax tree.
118      *
119      * @return Underlying AST.
120      */
121     @Nonnull public ParserRuleContext getAST() {
122         return tree;
123     }
124
125     /**
126      * Return the dependency information as extracted from the AST.
127      *
128      * FIXME: this method should be extracted into a public interface in the
129      *        model.api.repo class, relying solely on model.api types.
130      *
131      * @return Dependency information.
132      */
133     @Nonnull public YangModelDependencyInfo getDependencyInformation() {
134         return depInfo;
135     }
136
137     /**
138      * Return the semantically-equivalent text YANG text source.
139      *
140      * @return YANG text source
141      * @deprecated Used for migration purposes. Users are advised to use the
142      *             schema repository to acquire the representation of their
143      *             choice. Will be removed as soon as the migration is completed.
144      */
145     @Deprecated
146     @Nonnull public String getYangText() {
147         return text;
148     }
149 }