2 * Copyright (c) 2015 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.stmt.import_;
10 import com.google.common.base.MoreObjects;
11 import java.util.Objects;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.Revision;
16 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 final class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
32 implements ImportEffectiveStatement, ModuleImport {
34 private final String moduleName;
35 private final Revision revision;
36 private final SemVer semVer;
37 private final String prefix;
38 private final String description;
39 private final String reference;
41 ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
44 moduleName = ctx.coerceStatementArgument();
45 final Optional<String> prefixStmt = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class);
46 MissingSubstatementException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
47 "Prefix is mandatory substatement of import statement");
48 this.prefix = prefixStmt.get();
50 if (!ctx.isEnabledSemanticVersioning()) {
51 final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
52 RevisionDateEffectiveStatement.class);
53 this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
56 final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
57 ImportPrefixToSemVerSourceIdentifier.class, prefix);
58 revision = importedModuleIdentifier.getRevision().orElse(null);
59 semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
62 description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
63 reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
66 private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
68 * When 'revision-date' of an import is not specified in yang source, we
69 * need to find revision of imported module.
71 final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
72 SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
73 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
74 return importedModule.getRevision().orElse(null);
78 public String getModuleName() {
83 public Optional<Revision> getRevision() {
84 return Optional.ofNullable(revision);
88 public Optional<SemVer> getSemanticVersion() {
89 return Optional.ofNullable(semVer);
93 public String getPrefix() {
98 public Optional<String> getDescription() {
99 return Optional.ofNullable(description);
103 public Optional<String> getReference() {
104 return Optional.ofNullable(reference);
108 public int hashCode() {
109 return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
113 public boolean equals(final Object obj) {
120 if (getClass() != obj.getClass()) {
123 final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
124 return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
125 && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
126 && Objects.equals(description, other.description) && Objects.equals(reference, other.reference);
130 public String toString() {
131 return MoreObjects.toStringHelper(this).omitNullValues().add("moduleName", getModuleName())
132 .add("revision", revision).add("version", semVer).add("prefix", getPrefix())
133 .add("description", description).add("reference", reference).toString();