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