Eliminate YangModelDependencyInfo
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / YangIRSourceInfoExtractor.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 java.io.IOException;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.Revision;
14 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.common.YangVersion;
17 import org.opendaylight.yangtools.yang.ir.IRKeyword;
18 import org.opendaylight.yangtools.yang.ir.IRStatement;
19 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.BelongsTo;
23 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Import;
24 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Include;
25 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
27 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
28 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.ExplicitStatement;
30
31 /**
32  * Utility class for extract {@link SourceInfo} from a {@link YangIRSchemaSource}.
33  */
34 public final class YangIRSourceInfoExtractor {
35     private static final String BELONGS_TO = YangStmtMapping.BELONGS_TO.getStatementName().getLocalName();
36     private static final String IMPORT = YangStmtMapping.IMPORT.getStatementName().getLocalName();
37     private static final String INCLUDE = YangStmtMapping.INCLUDE.getStatementName().getLocalName();
38     private static final String MODULE = YangStmtMapping.MODULE.getStatementName().getLocalName();
39     private static final String NAMESPACE = YangStmtMapping.NAMESPACE.getStatementName().getLocalName();
40     private static final String PREFIX = YangStmtMapping.PREFIX.getStatementName().getLocalName();
41     private static final String REVISION = YangStmtMapping.REVISION.getStatementName().getLocalName();
42     private static final String REVISION_DATE = YangStmtMapping.REVISION_DATE.getStatementName().getLocalName();
43     private static final String SUBMODULE = YangStmtMapping.SUBMODULE.getStatementName().getLocalName();
44     private static final String YANG_VERSION = YangStmtMapping.YANG_VERSION.getStatementName().getLocalName();
45
46     private YangIRSourceInfoExtractor() {
47         // Hidden on purpose
48     }
49
50     /**
51      * Extracts {@link SourceInfo} from an intermediate representation root statement of a YANG model.
52      *
53      * @param source Schema source
54      * @return {@link SourceInfo}
55      * @throws IllegalArgumentException If the root statement is not a valid YANG module/submodule
56      */
57     public static @NonNull SourceInfo forIR(final YangIRSchemaSource source) {
58         return forIR(source.getRootStatement(), source.sourceId());
59     }
60
61     /**
62      * Extracts {@link SourceInfo} from an intermediate representation root statement of a YANG model.
63      *
64      * @param sourceId Source identifier, perhaps guessed from input name
65      * @param rootStatement root statement
66      * @return {@link SourceInfo}
67      * @throws IllegalArgumentException If the root statement is not a valid YANG module/submodule
68      */
69     public static @NonNull SourceInfo forIR(final IRStatement rootStatement, final SourceIdentifier sourceId) {
70         final var keyword = rootStatement.keyword();
71         if (!(keyword instanceof IRKeyword.Unqualified)) {
72             throw new IllegalArgumentException("Invalid root statement " + keyword);
73         }
74
75         final String arg = keyword.identifier();
76         if (MODULE.equals(arg)) {
77             return moduleForIR(rootStatement, sourceId);
78         }
79         if (SUBMODULE.equals(arg)) {
80             return submmoduleForIR(rootStatement, sourceId);
81         }
82         throw new IllegalArgumentException("Root of parsed AST must be either module or submodule");
83     }
84
85     /**
86      * Extracts {@link SourceInfo} from a {@link YangTextSource}. This parsing does not validate full YANG module, only
87      * parses header up to the revisions and imports.
88      *
89      * @param yangText {@link YangTextSource}
90      * @return {@link SourceInfo}
91      * @throws YangSyntaxErrorException If the resource does not pass syntactic analysis
92      * @throws IOException When the resource cannot be read
93      */
94     public static SourceInfo forYangText(final YangTextSource yangText)
95             throws IOException, YangSyntaxErrorException {
96         final var source = YangStatementStreamSource.create(yangText);
97         return forIR(source.rootStatement(), source.getIdentifier());
98     }
99
100     private static SourceInfo.@NonNull Module moduleForIR(final IRStatement root, final SourceIdentifier sourceId) {
101         final var builder = SourceInfo.Module.builder();
102         fill(builder, root, sourceId);
103         return builder
104             .setNamespace(root.statements().stream()
105                 .filter(stmt -> isStatement(stmt, NAMESPACE))
106                 .findFirst()
107                 .map(stmt -> safeStringArgument(sourceId, stmt, "namespace argument"))
108                 .map(XMLNamespace::of)
109                 .orElseThrow(() -> new IllegalArgumentException("No namespace statement in " + refOf(sourceId, root))))
110             .setPrefix(extractPrefix(root, sourceId))
111             .build();
112     }
113
114     private static SourceInfo.@NonNull Submodule submmoduleForIR(final IRStatement root,
115             final SourceIdentifier sourceId) {
116         final var builder = SourceInfo.Submodule.builder();
117         fill(builder, root, sourceId);
118         return builder
119             .setBelongsTo(root.statements().stream()
120                 .filter(stmt -> isStatement(stmt, BELONGS_TO))
121                 .findFirst()
122                 .map(stmt -> new BelongsTo(Unqualified.of(safeStringArgument(sourceId, stmt, "belongs-to module name")),
123                     extractPrefix(stmt, sourceId)))
124                 .orElseThrow(() -> new IllegalArgumentException("No belongs-to statement in " + refOf(sourceId, root))))
125             .build();
126     }
127
128     private static void fill(final SourceInfo.Builder<?, ?> builder, final IRStatement root,
129             final SourceIdentifier sourceId) {
130         builder.setName(Unqualified.of(safeStringArgument(sourceId, root, "module/submodule argument")));
131
132         root.statements().stream()
133             .filter(stmt -> isStatement(stmt, YANG_VERSION))
134             .findFirst()
135             .map(stmt -> safeStringArgument(sourceId, stmt, "yang-version argument"))
136             .map(YangVersion::forString)
137             .ifPresent(builder::setYangVersion);
138
139         root.statements().stream()
140             .filter(stmt -> isStatement(stmt, REVISION))
141             .map(stmt -> Revision.of(safeStringArgument(sourceId, stmt, "revision argument")))
142             .forEach(builder::addRevision);
143
144         root.statements().stream()
145             .filter(stmt -> isStatement(stmt, IMPORT))
146             .map(stmt -> new Import(Unqualified.of(safeStringArgument(sourceId, stmt, "import argument")),
147                 extractPrefix(stmt, sourceId), extractRevisionDate(stmt, sourceId)))
148             .forEach(builder::addImport);
149
150         root.statements().stream()
151             .filter(stmt -> isStatement(stmt, INCLUDE))
152             .map(stmt -> new Include(Unqualified.of(safeStringArgument(sourceId, stmt, "include argument")),
153                 extractRevisionDate(stmt, sourceId)))
154             .forEach(builder::addInclude);
155     }
156
157     private static @NonNull Unqualified extractPrefix(final IRStatement root, final SourceIdentifier sourceId) {
158         return root.statements().stream()
159             .filter(stmt -> isStatement(stmt, PREFIX))
160             .findFirst()
161             .map(stmt -> Unqualified.of(safeStringArgument(sourceId, stmt, "prefix argument")))
162             .orElseThrow(() -> new IllegalArgumentException("No prefix statement in " + refOf(sourceId, root)));
163     }
164
165     private static @Nullable Revision extractRevisionDate(final IRStatement root, final SourceIdentifier sourceId) {
166         return root.statements().stream()
167             .filter(stmt -> isStatement(stmt, REVISION_DATE))
168             .findFirst()
169             .map(stmt -> Revision.of(safeStringArgument(sourceId, stmt, "revision date argument")))
170             .orElse(null);
171     }
172
173     private static boolean isStatement(final IRStatement stmt, final String name) {
174         return stmt.keyword() instanceof IRKeyword.Unqualified keyword && name.equals(keyword.identifier());
175     }
176
177     private static @NonNull String safeStringArgument(final SourceIdentifier source, final IRStatement stmt,
178             final String desc) {
179         final var ref = refOf(source, stmt);
180         final var arg = stmt.argument();
181         if (arg == null) {
182             throw new IllegalArgumentException("Missing " + desc + " at " + ref);
183         }
184
185         // TODO: we probably need to understand yang version first....
186         return ArgumentContextUtils.rfc6020().stringFromStringContext(arg, ref);
187     }
188
189     private static StatementSourceReference refOf(final SourceIdentifier source, final IRStatement stmt) {
190         return ExplicitStatement.atPosition(source.name().getLocalName(), stmt.startLine(), stmt.startColumn() + 1);
191     }
192 }