2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/eplv10.html
7 package org.opendaylight.yangtools.yang.parser.impl.util;
9 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getArgumentString;
10 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getFirstContext;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableSet;
14 import java.io.InputStream;
15 import java.util.Date;
16 import java.util.List;
17 import org.antlr.v4.runtime.ParserRuleContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Belongs_to_stmtContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Include_stmtContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext;
26 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
33 * Helper transfer object which holds basic and dependency information for YANG
38 * There are two concrete implementations of this interface:
40 * <li>{@link ModuleDependencyInfo} - Dependency information for module</li>
41 * <li>{@link SubmoduleDependencyInfo} - Dependency information for submodule</li>
44 * @see ModuleDependencyInfo
45 * @see SubmoduleDependencyInfo
49 public abstract class YangModelDependencyInfo {
51 private final String name;
52 private final String formattedRevision;
53 private final Date revision;
54 private final ImmutableSet<ModuleImport> submoduleIncludes;
55 private final ImmutableSet<ModuleImport> moduleImports;
56 private final ImmutableSet<ModuleImport> dependencies;
58 YangModelDependencyInfo(final String name, final String formattedRevision,
59 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
61 this.formattedRevision = formattedRevision;
62 this.revision = formattedRevision == null ? null : QName.parseRevision(formattedRevision);
63 this.moduleImports = imports;
64 this.submoduleIncludes = includes;
65 this.dependencies = ImmutableSet.<ModuleImport> builder()
66 .addAll(moduleImports)
67 .addAll(submoduleIncludes)
72 * Returns immutable collection of all module imports.
74 * This collection contains both <code>import</code> statements
75 * and <code>include</code> statements for submodules.
77 * @return Immutable collection of imports.
79 public ImmutableSet<ModuleImport> getDependencies() {
88 public String getName() {
93 * Returns formatted revision string
95 * @return formatted revision string
97 public String getFormattedRevision() {
98 return formattedRevision;
111 public int hashCode() {
112 final int prime = 31;
114 result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
115 result = prime * result + ((name == null) ? 0 : name.hashCode());
120 public boolean equals(final Object obj) {
127 if (!(obj instanceof YangModelDependencyInfo)) {
130 YangModelDependencyInfo other = (YangModelDependencyInfo) obj;
131 if (formattedRevision == null) {
132 if (other.formattedRevision != null) {
135 } else if (!formattedRevision.equals(other.formattedRevision)) {
139 if (other.name != null) {
142 } else if (!name.equals(other.name)) {
149 * Extracts {@link YangModelDependencyInfo} from an abstract syntax tree
152 * @param tree Abstract syntax tree
153 * @return {@link YangModelDependencyInfo}
154 * @throws YangSyntaxErrorException
155 * If the AST is not a valid YANG module/submodule
157 public static YangModelDependencyInfo fromAST(final String name, final ParserRuleContext tree) throws YangSyntaxErrorException {
158 final Optional<Module_stmtContext> moduleCtx = getFirstContext(tree, Module_stmtContext.class);
159 if (moduleCtx.isPresent()) {
160 return parseModuleContext(moduleCtx.get());
163 final Optional<Submodule_stmtContext> submoduleCtx = getFirstContext(tree, Submodule_stmtContext.class);
164 if (submoduleCtx.isPresent()) {
165 return parseSubmoduleContext(submoduleCtx.get());
168 throw new YangSyntaxErrorException(name, 0, 0, "Unknown YANG text type");
172 * Extracts {@link YangModelDependencyInfo} from input stream
173 * containing YANG model.
175 * This parsing does not validate full YANG module, only
176 * parses header up to the revisions and imports.
179 * Opened Input stream containing text source of YANG model
180 * @return {@link YangModelDependencyInfo}
181 * @throws IllegalArgumentException
182 * If input stream is not valid YANG stream
184 public static YangModelDependencyInfo fromInputStream(final InputStream yangStream) {
185 YangContext yangContext = YangParserImpl.parseStreamWithoutErrorListeners(yangStream);
187 Optional<Module_stmtContext> moduleCtx = getFirstContext(yangContext, Module_stmtContext.class);
188 if (moduleCtx.isPresent()) {
189 return parseModuleContext(moduleCtx.get());
191 Optional<Submodule_stmtContext> submoduleCtx = getFirstContext(yangContext, Submodule_stmtContext.class);
192 if (submoduleCtx.isPresent()) {
193 return parseSubmoduleContext(submoduleCtx.get());
195 throw new IllegalArgumentException("Supplied stream is not valid yang file.");
198 private static YangModelDependencyInfo parseModuleContext(final Module_stmtContext module) {
199 String name = getArgumentString(module);
200 String latestRevision = getLatestRevision(module.revision_stmts());
201 ImmutableSet<ModuleImport> imports = parseImports(module.linkage_stmts().import_stmt());
202 ImmutableSet<ModuleImport> includes = parseIncludes(module.linkage_stmts().include_stmt());
204 return new ModuleDependencyInfo(name, latestRevision, imports, includes);
207 private static ImmutableSet<ModuleImport> parseImports(final List<Import_stmtContext> importStatements) {
208 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
209 for (Import_stmtContext importStmt : importStatements) {
210 String moduleName = getArgumentString(importStmt);
211 Date revision = getRevision(importStmt.revision_date_stmt());
212 builder.add(new ModuleImportImpl(moduleName, revision));
214 return builder.build();
217 public static String getLatestRevision(final Revision_stmtsContext revisionStmts) {
218 List<Revision_stmtContext> revisions = revisionStmts.getRuleContexts(Revision_stmtContext.class);
219 String latestRevision = null;
220 for (Revision_stmtContext revisionStmt : revisions) {
221 String currentRevision = getArgumentString(revisionStmt);
222 if (latestRevision == null || latestRevision.compareTo(currentRevision) == -1) {
223 latestRevision = currentRevision;
226 return latestRevision;
229 private static YangModelDependencyInfo parseSubmoduleContext(final Submodule_stmtContext submodule) {
230 String name = getArgumentString(submodule);
231 Belongs_to_stmtContext belongsToStmt = submodule.submodule_header_stmts().belongs_to_stmt(0);
232 String belongsTo = getArgumentString(belongsToStmt);
234 String latestRevision = getLatestRevision(submodule.revision_stmts());
235 ImmutableSet<ModuleImport> imports = parseImports(submodule.linkage_stmts().import_stmt());
236 ImmutableSet<ModuleImport> includes = parseIncludes(submodule.linkage_stmts().include_stmt());
238 return new SubmoduleDependencyInfo(name, latestRevision, belongsTo, imports, includes);
241 private static ImmutableSet<ModuleImport> parseIncludes(final List<Include_stmtContext> importStatements) {
242 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
243 for (Include_stmtContext importStmt : importStatements) {
244 String moduleName = getArgumentString(importStmt);
245 Date revision = getRevision(importStmt.revision_date_stmt());
246 builder.add(new ModuleImportImpl(moduleName, revision));
248 return builder.build();
251 private static Date getRevision(final Revision_date_stmtContext revisionDateStmt) {
252 if (revisionDateStmt == null) {
255 String formatedDate = getArgumentString(revisionDateStmt);
256 return QName.parseRevision(formatedDate);
261 * Dependency information for YANG module.
264 public static final class ModuleDependencyInfo extends YangModelDependencyInfo {
266 private ModuleDependencyInfo(final String name, final String latestRevision,
267 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
268 super(name, latestRevision, imports, includes);
272 public String toString() {
273 return "Module [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + getDependencies()
280 * Dependency information for submodule, also provides name
284 public static final class SubmoduleDependencyInfo extends YangModelDependencyInfo {
286 private final String belongsTo;
288 private SubmoduleDependencyInfo(final String name, final String latestRevision, final String belongsTo,
289 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
290 super(name, latestRevision, imports, includes);
291 this.belongsTo = belongsTo;
295 * Returns name of parent module.
298 public String getParentModule() {
303 public String toString() {
304 return "Submodule [name=" + getName() + ", revision=" + getRevision() + ", dependencies="
305 + getDependencies() + "]";
310 * Utility implementation of {@link ModuleImport} to be used by
311 * {@link YangModelDependencyInfo}.
314 private static final class ModuleImportImpl implements ModuleImport {
316 private final Date revision;
317 private final String name;
319 public ModuleImportImpl(final String moduleName, final Date revision) {
320 this.name = moduleName;
321 this.revision = revision;
325 public String getModuleName() {
330 public Date getRevision() {
331 return this.revision;
335 public String getPrefix() {
340 public int hashCode() {
341 final int prime = 31;
343 result = prime * result + ((name == null) ? 0 : name.hashCode());
344 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
349 public boolean equals(final Object obj) {
356 if (getClass() != obj.getClass()) {
359 ModuleImportImpl other = (ModuleImportImpl) obj;
361 if (other.name != null) {
364 } else if (!name.equals(other.name)) {
367 if (revision == null) {
368 if (other.revision != null) {
371 } else if (!revision.equals(other.revision)) {
378 public String toString() {
379 return "ModuleImportImpl [name=" + name + ", revision=" + QName.formattedRevision(revision) + "]";