2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
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.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDeclaration;
21 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.BelongsTo;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Import;
23 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Include;
24 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
26 import org.opendaylight.yangtools.yang.model.spi.meta.StatementDeclarations;
27 import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
28 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 * Utility class for extract {@link SourceInfo} from a {@link YangIRSource}.
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();
46 private YangIRSourceInfoExtractor() {
51 * Extracts {@link SourceInfo} from an intermediate representation root statement of a YANG model.
53 * @param source Schema source
54 * @return {@link SourceInfo}
55 * @throws IllegalArgumentException If the root statement is not a valid YANG module/submodule
57 public static @NonNull SourceInfo forIR(final YangIRSource source) {
58 return forIR(source.statement(), source.sourceId());
62 * Extracts {@link SourceInfo} from an intermediate representation root statement of a YANG model.
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
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);
75 final String arg = keyword.identifier();
76 if (MODULE.equals(arg)) {
77 return moduleForIR(rootStatement, sourceId);
79 if (SUBMODULE.equals(arg)) {
80 return submmoduleForIR(rootStatement, sourceId);
82 throw new IllegalArgumentException("Root of parsed AST must be either module or submodule");
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.
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
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());
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);
104 .setNamespace(root.statements().stream()
105 .filter(stmt -> isStatement(stmt, NAMESPACE))
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))
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);
119 .setBelongsTo(root.statements().stream()
120 .filter(stmt -> isStatement(stmt, BELONGS_TO))
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))))
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")));
132 root.statements().stream()
133 .filter(stmt -> isStatement(stmt, YANG_VERSION))
135 .map(stmt -> safeStringArgument(sourceId, stmt, "yang-version argument"))
136 .map(YangVersion::forString)
137 .ifPresent(builder::setYangVersion);
139 root.statements().stream()
140 .filter(stmt -> isStatement(stmt, REVISION))
141 .map(stmt -> Revision.of(safeStringArgument(sourceId, stmt, "revision argument")))
142 .forEach(builder::addRevision);
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);
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);
157 private static @NonNull Unqualified extractPrefix(final IRStatement root, final SourceIdentifier sourceId) {
158 return root.statements().stream()
159 .filter(stmt -> isStatement(stmt, PREFIX))
161 .map(stmt -> Unqualified.of(safeStringArgument(sourceId, stmt, "prefix argument")))
162 .orElseThrow(() -> new IllegalArgumentException("No prefix statement in " + refOf(sourceId, root)));
165 private static @Nullable Revision extractRevisionDate(final IRStatement root, final SourceIdentifier sourceId) {
166 return root.statements().stream()
167 .filter(stmt -> isStatement(stmt, REVISION_DATE))
169 .map(stmt -> Revision.of(safeStringArgument(sourceId, stmt, "revision date argument")))
173 private static boolean isStatement(final IRStatement stmt, final String name) {
174 return stmt.keyword() instanceof IRKeyword.Unqualified keyword && name.equals(keyword.identifier());
177 private static @NonNull String safeStringArgument(final SourceIdentifier source, final IRStatement stmt,
179 final var ref = refOf(source, stmt);
180 final var arg = stmt.argument();
182 throw new IllegalArgumentException("Missing " + desc + " at " + ref);
185 // TODO: we probably need to understand yang version first....
186 return ArgumentContextUtils.rfc6020().stringFromStringContext(arg, ref);
189 private static StatementDeclaration.InText refOf(final SourceIdentifier source, final IRStatement stmt) {
190 return StatementDeclarations.inText(source.name().getLocalName(), stmt.startLine(), stmt.startColumn() + 1);