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