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 = 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);
200 // getArgumentString(module.module_header_stmts().prefix_stmt(0));
201 String namespace = getArgumentString(module.module_header_stmts().namespace_stmt(0));
202 String latestRevision = getLatestRevision(module.revision_stmts());
203 ImmutableSet<ModuleImport> imports = parseImports(module.linkage_stmts().import_stmt());
204 ImmutableSet<ModuleImport> includes = parseIncludes(module.linkage_stmts().include_stmt());
206 return new ModuleDependencyInfo(name, latestRevision, namespace, imports, includes);
209 private static ImmutableSet<ModuleImport> parseImports(final List<Import_stmtContext> importStatements) {
210 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
211 for (Import_stmtContext importStmt : importStatements) {
212 String moduleName = getArgumentString(importStmt);
213 Date revision = getRevision(importStmt.revision_date_stmt());
214 builder.add(new ModuleImportImpl(moduleName, revision));
216 return builder.build();
219 public static String getLatestRevision(final Revision_stmtsContext revision_stmts) {
220 List<Revision_stmtContext> revisions = revision_stmts.getRuleContexts(Revision_stmtContext.class);
221 String latestRevision = null;
222 for (Revision_stmtContext revisionStmt : revisions) {
223 String currentRevision = getArgumentString(revisionStmt);
224 if (latestRevision == null || latestRevision.compareTo(currentRevision) == -1) {
225 latestRevision = currentRevision;
228 return latestRevision;
231 private static YangModelDependencyInfo parseSubmoduleContext(final Submodule_stmtContext submodule) {
232 String name = getArgumentString(submodule);
233 Belongs_to_stmtContext belongsToStmt = submodule.submodule_header_stmts().belongs_to_stmt(0);
234 String belongsTo = getArgumentString(belongsToStmt);
236 String latestRevision = getLatestRevision(submodule.revision_stmts());
237 ImmutableSet<ModuleImport> imports = parseImports(submodule.linkage_stmts().import_stmt());
238 ImmutableSet<ModuleImport> includes = parseIncludes(submodule.linkage_stmts().include_stmt());
240 return new SubmoduleDependencyInfo(name, latestRevision, belongsTo, imports, includes);
243 private static ImmutableSet<ModuleImport> parseIncludes(final List<Include_stmtContext> importStatements) {
244 ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
245 for (Include_stmtContext importStmt : importStatements) {
246 String moduleName = getArgumentString(importStmt);
247 Date revision = getRevision(importStmt.revision_date_stmt());
248 builder.add(new ModuleImportImpl(moduleName, revision));
250 return builder.build();
253 private static Date getRevision(final Revision_date_stmtContext revision_date_stmt) {
254 if (revision_date_stmt == null) {
257 String formatedDate = getArgumentString(revision_date_stmt);
258 return QName.parseRevision(formatedDate);
263 * Dependency information for YANG module.
266 public static final class ModuleDependencyInfo extends YangModelDependencyInfo {
268 private ModuleDependencyInfo(final String name, final String latestRevision, final String namespace,
269 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
270 super(name, latestRevision, imports, includes);
274 public String toString() {
275 return "Module [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + getDependencies()
282 * Dependency information for submodule, also provides name
286 public static final class SubmoduleDependencyInfo extends YangModelDependencyInfo {
288 private final String belongsTo;
291 * Returns name of parent module.
294 public String getParentModule() {
298 private SubmoduleDependencyInfo(final String name, final String latestRevision, final String belongsTo,
299 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
300 super(name, latestRevision, imports, includes);
301 this.belongsTo = belongsTo;
305 public String toString() {
306 return "Submodule [name=" + getName() + ", revision=" + getRevision() + ", dependencies="
307 + getDependencies() + "]";
312 * Utility implementation of {@link ModuleImport} to be used by
313 * {@link YangModelDependencyInfo}.
316 private static final class ModuleImportImpl implements ModuleImport {
318 private final Date revision;
319 private final String name;
321 public ModuleImportImpl(final String moduleName, final Date revision) {
322 this.name = moduleName;
323 this.revision = revision;
327 public String getModuleName() {
332 public Date getRevision() {
333 return this.revision;
337 public String getPrefix() {
342 public int hashCode() {
343 final int prime = 31;
345 result = prime * result + ((name == null) ? 0 : name.hashCode());
346 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
351 public boolean equals(final Object obj) {
358 if (getClass() != obj.getClass()) {
361 ModuleImportImpl other = (ModuleImportImpl) obj;
363 if (other.name != null) {
366 } else if (!name.equals(other.name)) {
369 if (revision == null) {
370 if (other.revision != null) {
373 } else if (!revision.equals(other.revision)) {
380 public String toString() {
381 return "ModuleImportImpl [name=" + name + ", revision=" + QName.formattedRevision(revision) + "]";