X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2Futil%2FYangModelDependencyInfo.java;h=fcf0b8924c15fc3ab81a6e908b4d77173683f412;hb=0dab204f1b81c5f0f500bc7f4bc3301b321288c3;hp=6530c92510c8d6aabd43eb5640a4ae146b5c71ca;hpb=a243f452faae458db1b5b19d77c61981c5e38c9c;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfo.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfo.java index 6530c92510..fcf0b8924c 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfo.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfo.java @@ -1,19 +1,19 @@ /* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/eplv10.html */ package org.opendaylight.yangtools.yang.parser.impl.util; -import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getArgumentString; -import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getFirstContext; +import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getArgumentString; +import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getFirstContext; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableSet; import java.io.InputStream; import java.util.Date; import java.util.List; - import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Belongs_to_stmtContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Include_stmtContext; @@ -27,9 +27,22 @@ import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.ModuleImport; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; - +/** + * Helper transfer object which holds basic and dependency information for YANG + * model. + * + * + * + * There are two concrete implementations of this interface: + * + * + * @see ModuleDependencyInfo + * @see SubmoduleDependencyInfo + * + */ public abstract class YangModelDependencyInfo { private final String name; @@ -39,8 +52,8 @@ public abstract class YangModelDependencyInfo { private final ImmutableSet moduleImports; private final ImmutableSet dependencies; - public YangModelDependencyInfo(final String name, final String formattedRevision, final ImmutableSet imports, - final ImmutableSet includes) { + YangModelDependencyInfo(final String name, final String formattedRevision, + final ImmutableSet imports, final ImmutableSet includes) { this.name = name; this.formattedRevision = formattedRevision; this.revision = QName.parseRevision(formattedRevision); @@ -52,19 +65,42 @@ public abstract class YangModelDependencyInfo { .build(); } + /** + * Returns immutable collection of all module imports. + * + * This collection contains both import statements + * and include statements for submodules. + * + * @return Immutable collection of imports. + */ public ImmutableSet getDependencies() { return dependencies; } + /** + * Returns model name + * + * @return model name + */ public String getName() { return name; } + /** + * Returns formatted revision string + * + * @return formatted revision string + */ public String getFormattedRevision() { return formattedRevision; } - public Date getRevision() { + /** + * Returns revision + * + * @return revision + */ + Date getRevision() { return revision; } @@ -106,33 +142,46 @@ public abstract class YangModelDependencyInfo { return true; } + /** + * Extracts {@link YangModelDependencyInfo} from input stream + * containing YANG model. + * + * This parsing does not validate full YANG module, only + * parses header up to the revisions and imports. + * + * @param yangStream + * Opened Input stream containing text source of YANG model + * @return {@link YangModelDependencyInfo} + * @throws IllegalArgumentException + * If input stream is not valid YANG stream + */ public static YangModelDependencyInfo fromInputStream(final InputStream yangStream) { YangContext yangContext = YangParserImpl.parseStreamWithoutErrorListeners(yangStream); Optional moduleCtx = getFirstContext(yangContext, Module_stmtContext.class); if (moduleCtx.isPresent()) { - return fromModuleContext(moduleCtx.get()); + return parseModuleContext(moduleCtx.get()); } Optional submoduleCtx = getFirstContext(yangContext, Submodule_stmtContext.class); if (submoduleCtx.isPresent()) { - return fromSubmoduleContext(submoduleCtx.get()); + return parseSubmoduleContext(submoduleCtx.get()); } throw new IllegalArgumentException("Supplied stream is not valid yang file."); } - private static YangModelDependencyInfo fromModuleContext(final Module_stmtContext module) { + private static YangModelDependencyInfo parseModuleContext(final Module_stmtContext module) { String name = getArgumentString(module); // String prefix = // getArgumentString(module.module_header_stmts().prefix_stmt(0)); String namespace = getArgumentString(module.module_header_stmts().namespace_stmt(0)); String latestRevision = getLatestRevision(module.revision_stmts()); - ImmutableSet imports = getImports(module.linkage_stmts().import_stmt()); - ImmutableSet includes = getIncludes(module.linkage_stmts().include_stmt()); + ImmutableSet imports = parseImports(module.linkage_stmts().import_stmt()); + ImmutableSet includes = parseIncludes(module.linkage_stmts().include_stmt()); return new ModuleDependencyInfo(name, latestRevision, namespace, imports, includes); } - private static ImmutableSet getImports(final List importStatements) { + private static ImmutableSet parseImports(final List importStatements) { ImmutableSet.Builder builder = ImmutableSet.builder(); for (Import_stmtContext importStmt : importStatements) { String moduleName = getArgumentString(importStmt); @@ -154,19 +203,19 @@ public abstract class YangModelDependencyInfo { return latestRevision; } - private static YangModelDependencyInfo fromSubmoduleContext(final Submodule_stmtContext submodule) { + private static YangModelDependencyInfo parseSubmoduleContext(final Submodule_stmtContext submodule) { String name = getArgumentString(submodule); Belongs_to_stmtContext belongsToStmt = submodule.submodule_header_stmts().belongs_to_stmt(0); String belongsTo = getArgumentString(belongsToStmt); String latestRevision = getLatestRevision(submodule.revision_stmts()); - ImmutableSet imports = getImports(submodule.linkage_stmts().import_stmt()); - ImmutableSet includes = getIncludes(submodule.linkage_stmts().include_stmt()); + ImmutableSet imports = parseImports(submodule.linkage_stmts().import_stmt()); + ImmutableSet includes = parseIncludes(submodule.linkage_stmts().include_stmt()); return new SubmoduleDependencyInfo(name, latestRevision, belongsTo, imports, includes); } - private static ImmutableSet getIncludes(final List importStatements) { + private static ImmutableSet parseIncludes(final List importStatements) { ImmutableSet.Builder builder = ImmutableSet.builder(); for (Include_stmtContext importStmt : importStatements) { String moduleName = getArgumentString(importStmt); @@ -184,6 +233,11 @@ public abstract class YangModelDependencyInfo { return QName.parseRevision(formatedDate); } + /** + * + * Dependency information for YANG module. + * + */ public static final class ModuleDependencyInfo extends YangModelDependencyInfo { private ModuleDependencyInfo(final String name, final String latestRevision, final String namespace, @@ -193,15 +247,25 @@ public abstract class YangModelDependencyInfo { @Override public String toString() { - return "Module [name=" + getName() + ", revision=" + getRevision() - + ", dependencies=" + getDependencies() + "]"; + return "Module [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + getDependencies() + + "]"; } } + /** + * + * Dependency information for submodule, also provides name + * for parent module. + * + */ public static final class SubmoduleDependencyInfo extends YangModelDependencyInfo { private final String belongsTo; + /** + * Returns name of parent module. + * + */ public String getParentModule() { return belongsTo; } @@ -214,11 +278,16 @@ public abstract class YangModelDependencyInfo { @Override public String toString() { - return "Submodule [name=" + getName() + ", revision=" + getRevision() - + ", dependencies=" + getDependencies() + "]"; + return "Submodule [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + + getDependencies() + "]"; } } + /** + * Utility implementation of {@link ModuleImport} to be used by + * {@link YangModelDependencyInfo}. + * + */ private static final class ModuleImportImpl implements ModuleImport { private final Date revision;