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
48 public abstract class YangModelDependencyInfo {
50 private final String name;
51 private final String formattedRevision;
52 private final Date revision;
53 private final ImmutableSet<ModuleImport> submoduleIncludes;
54 private final ImmutableSet<ModuleImport> moduleImports;
55 private final ImmutableSet<ModuleImport> dependencies;
57 YangModelDependencyInfo(final String name, final String formattedRevision,
58 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
60 this.formattedRevision = formattedRevision;
61 this.revision = formattedRevision == null ? null : QName.parseRevision(formattedRevision);
62 this.moduleImports = imports;
63 this.submoduleIncludes = includes;
64 this.dependencies = ImmutableSet.<ModuleImport> builder() //
65 .addAll(moduleImports) //
66 .addAll(submoduleIncludes) //
71 * Returns immutable collection of all module imports.
73 * This collection contains both <code>import</code> statements
74 * and <code>include</code> statements for submodules.
76 * @return Immutable collection of imports.
78 public ImmutableSet<ModuleImport> getDependencies() {
87 public String getName() {
92 * Returns formatted revision string
94 * @return formatted revision string
96 public String getFormattedRevision() {
97 return formattedRevision;
110 public int hashCode() {
111 final int prime = 31;
113 result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
114 result = prime * result + ((name == null) ? 0 : name.hashCode());
119 public boolean equals(final Object obj) {
126 if (!(obj instanceof YangModelDependencyInfo)) {
129 YangModelDependencyInfo other = (YangModelDependencyInfo) obj;
130 if (formattedRevision == null) {
131 if (other.formattedRevision != null) {
134 } else if (!formattedRevision.equals(other.formattedRevision)) {
138 if (other.name != null) {
141 } else if (!name.equals(other.name)) {
148 * Extracts {@link YangModelDependencyInfo} from an abstract syntax tree
151 * @param tree Abstract syntax tree
152 * @return {@link YangModelDependencyInfo}
153 * @throws YangSyntaxErrorException
154 * If the AST is not a valid YANG module/submodule
156 public static YangModelDependencyInfo fromAST(final String name, final ParserRuleContext tree) throws YangSyntaxErrorException {
157 final Optional<Module_stmtContext> moduleCtx = getFirstContext(tree, Module_stmtContext.class);
158 if (moduleCtx.isPresent()) {
159 return parseModuleContext(moduleCtx.get());
162 final Optional<Submodule_stmtContext> submoduleCtx = getFirstContext(tree, Submodule_stmtContext.class);
163 if (submoduleCtx.isPresent()) {
164 return parseSubmoduleContext(submoduleCtx.get());
167 throw new YangSyntaxErrorException(name, 0, 0, "Unknown YANG text type");
171 * Extracts {@link YangModelDependencyInfo} from input stream
172 * containing YANG model.
174 * This parsing does not validate full YANG module, only
175 * parses header up to the revisions and imports.
178 * Opened Input stream containing text source of YANG model
179 * @return {@link YangModelDependencyInfo}
180 * @throws IllegalArgumentException
181 * If input stream is not valid YANG stream
183 public static YangModelDependencyInfo fromInputStream(final InputStream yangStream) {
184 YangContext yangContext = YangParserImpl.parseStreamWithoutErrorListeners(yangStream);
186 Optional<Module_stmtContext> moduleCtx = getFirstContext(yangContext, Module_stmtContext.class);
187 if (moduleCtx.isPresent()) {
188 return parseModuleContext(moduleCtx.get());
190 Optional<Submodule_stmtContext> submoduleCtx = getFirstContext(yangContext, Submodule_stmtContext.class);
191 if (submoduleCtx.isPresent()) {
192 return parseSubmoduleContext(submoduleCtx.get());
194 throw new IllegalArgumentException("Supplied stream is not valid yang file.");
197 private static YangModelDependencyInfo parseModuleContext(final Module_stmtContext module) {
198 String name = getArgumentString(module);
199 String latestRevision = getLatestRevision(module.revision_stmts());
200 ImmutableSet<ModuleImport> imports = parseImports(module.linkage_stmts().import_stmt());
201 ImmutableSet<ModuleImport> includes = parseIncludes(module.linkage_stmts().include_stmt());
203 return new ModuleDependencyInfo(name, latestRevision, imports, includes);
206 private static ImmutableSet<ModuleImport> parseImports(final List<Import_stmtContext> importStatements) {
207 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
208 for (Import_stmtContext importStmt : importStatements) {
209 String moduleName = getArgumentString(importStmt);
210 Date revision = getRevision(importStmt.revision_date_stmt());
211 builder.add(new ModuleImportImpl(moduleName, revision));
213 return builder.build();
216 public static String getLatestRevision(final Revision_stmtsContext revision_stmts) {
217 List<Revision_stmtContext> revisions = revision_stmts.getRuleContexts(Revision_stmtContext.class);
218 String latestRevision = null;
219 for (Revision_stmtContext revisionStmt : revisions) {
220 String currentRevision = getArgumentString(revisionStmt);
221 if (latestRevision == null || latestRevision.compareTo(currentRevision) == -1) {
222 latestRevision = currentRevision;
225 return latestRevision;
228 private static YangModelDependencyInfo parseSubmoduleContext(final Submodule_stmtContext submodule) {
229 String name = getArgumentString(submodule);
230 Belongs_to_stmtContext belongsToStmt = submodule.submodule_header_stmts().belongs_to_stmt(0);
231 String belongsTo = getArgumentString(belongsToStmt);
233 String latestRevision = getLatestRevision(submodule.revision_stmts());
234 ImmutableSet<ModuleImport> imports = parseImports(submodule.linkage_stmts().import_stmt());
235 ImmutableSet<ModuleImport> includes = parseIncludes(submodule.linkage_stmts().include_stmt());
237 return new SubmoduleDependencyInfo(name, latestRevision, belongsTo, imports, includes);
240 private static ImmutableSet<ModuleImport> parseIncludes(final List<Include_stmtContext> importStatements) {
241 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
242 for (Include_stmtContext importStmt : importStatements) {
243 String moduleName = getArgumentString(importStmt);
244 Date revision = getRevision(importStmt.revision_date_stmt());
245 builder.add(new ModuleImportImpl(moduleName, revision));
247 return builder.build();
250 private static Date getRevision(final Revision_date_stmtContext revision_date_stmt) {
251 if (revision_date_stmt == null) {
254 String formatedDate = getArgumentString(revision_date_stmt);
255 return QName.parseRevision(formatedDate);
260 * Dependency information for YANG module.
263 public static final class ModuleDependencyInfo extends YangModelDependencyInfo {
265 private ModuleDependencyInfo(final String name, final String latestRevision,
266 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
267 super(name, latestRevision, imports, includes);
271 public String toString() {
272 return "Module [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + getDependencies()
279 * Dependency information for submodule, also provides name
283 public static final class SubmoduleDependencyInfo extends YangModelDependencyInfo {
285 private final String belongsTo;
288 * Returns name of parent module.
291 public String getParentModule() {
295 private SubmoduleDependencyInfo(final String name, final String latestRevision, final String belongsTo,
296 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
297 super(name, latestRevision, imports, includes);
298 this.belongsTo = belongsTo;
302 public String toString() {
303 return "Submodule [name=" + getName() + ", revision=" + getRevision() + ", dependencies="
304 + getDependencies() + "]";
309 * Utility implementation of {@link ModuleImport} to be used by
310 * {@link YangModelDependencyInfo}.
313 private static final class ModuleImportImpl implements ModuleImport {
315 private final Date revision;
316 private final String name;
318 public ModuleImportImpl(final String moduleName, final Date revision) {
319 this.name = moduleName;
320 this.revision = revision;
324 public String getModuleName() {
329 public Date getRevision() {
330 return this.revision;
334 public String getPrefix() {
339 public int hashCode() {
340 final int prime = 31;
342 result = prime * result + ((name == null) ? 0 : name.hashCode());
343 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
348 public boolean equals(final Object obj) {
355 if (getClass() != obj.getClass()) {
358 ModuleImportImpl other = (ModuleImportImpl) obj;
360 if (other.name != null) {
363 } else if (!name.equals(other.name)) {
366 if (revision == null) {
367 if (other.revision != null) {
370 } else if (!revision.equals(other.revision)) {
377 public String toString() {
378 return "ModuleImportImpl [name=" + name + ", revision=" + QName.formattedRevision(revision) + "]";