Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / 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.rfc7950.repo;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import org.antlr.v4.runtime.ParserRuleContext;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
18 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
19 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
21 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23
24 /**
25  * Abstract Syntax Tree representation of a schema source. This representation is internal to the YANG parser
26  * implementation, as it relies on ANTLR types.
27  *
28  * <p>
29  * Instances of this representation are used for caching purposes, as they are a natural intermediate step in YANG text
30  * processing pipeline: the text has been successfully parsed, so we know it is syntactically correct. It also passes
31  * basic semantic validation and we were able to extract dependency information.
32  */
33 @Beta
34 public final class ASTSchemaSource extends AbstractIdentifiable<SourceIdentifier>
35         implements SchemaSourceRepresentation {
36     private final @NonNull YangModelDependencyInfo depInfo;
37     private final @NonNull SemVerSourceIdentifier semVerId;
38     private final @NonNull ParserRuleContext tree;
39     private final @Nullable String symbolicName;
40
41     private ASTSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull SemVerSourceIdentifier semVerId,
42             final @NonNull ParserRuleContext tree, final @NonNull YangModelDependencyInfo depInfo,
43             @Nullable final String symbolicName) {
44         super(identifier);
45         this.depInfo = requireNonNull(depInfo);
46         this.tree = requireNonNull(tree);
47         this.semVerId = requireNonNull(semVerId);
48         this.symbolicName = symbolicName;
49     }
50
51     /**
52      * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
53      * to acquire dependency information.
54      *
55      * @param symbolicName
56      *            Symbolic name
57      * @param identifier
58      *            SourceIdentifier of YANG schema source.
59      * @param tree
60      *            ANTLR abstract syntax tree
61      * @return A new representation instance.
62      * @throws YangSyntaxErrorException
63      *             if we fail to extract dependency information.
64      */
65     static @NonNull ASTSchemaSource create(final @NonNull SourceIdentifier identifier,
66             final @Nullable String symbolicName, final @NonNull ParserRuleContext tree)
67                     throws YangSyntaxErrorException {
68         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier, tree);
69         final SourceIdentifier id = getSourceId(depInfo);
70
71         final SemVerSourceIdentifier semVerId;
72         if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
73             semVerId = (SemVerSourceIdentifier) identifier;
74         } else {
75             semVerId = getSemVerSourceId(depInfo);
76         }
77
78         return new ASTSchemaSource(id, semVerId, tree, depInfo, symbolicName);
79     }
80
81     @Override
82     public Optional<String> getSymbolicName() {
83         return Optional.ofNullable(symbolicName);
84     }
85
86     public @NonNull SemVerSourceIdentifier getSemVerIdentifier() {
87         return semVerId;
88     }
89
90     @Override
91     public Class<? extends SchemaSourceRepresentation> getType() {
92         return ASTSchemaSource.class;
93     }
94
95     /**
96      * Return the underlying abstract syntax tree.
97      *
98      * @return Underlying AST.
99      */
100     public @NonNull ParserRuleContext getAST() {
101         return tree;
102     }
103
104     /**
105      * Return the dependency information as extracted from the AST.
106      *
107      * @return Dependency information.
108      */
109     // FIXME: this method should be extracted into a public interface in the model.api.repo class, relying solely
110     //        on model.api types.
111     public @NonNull YangModelDependencyInfo getDependencyInformation() {
112         return depInfo;
113     }
114
115     private static @NonNull SourceIdentifier getSourceId(final @NonNull YangModelDependencyInfo depInfo) {
116         final String name = depInfo.getName();
117         return depInfo.getFormattedRevision() == null ? RevisionSourceIdentifier.create(name)
118                 : RevisionSourceIdentifier.create(name, depInfo.getRevision());
119     }
120
121     private static @NonNull SemVerSourceIdentifier getSemVerSourceId(final @NonNull YangModelDependencyInfo depInfo) {
122         return depInfo.getFormattedRevision() == null
123                 ? SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getSemanticVersion().orElse(null))
124                         : SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getRevision(),
125                             depInfo.getSemanticVersion().orElse(null));
126     }
127 }