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.ImportEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedNodeWithoutStatus;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 final class ImportEffectiveStatementImpl extends AbstractEffectiveDocumentedNodeWithoutStatus<String, ImportStatement>
30 implements ImportEffectiveStatement, ModuleImport {
32 private final String moduleName;
33 private final Revision revision;
34 private final SemVer semVer;
35 private final String prefix;
37 ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
40 moduleName = ctx.coerceStatementArgument();
41 final Optional<String> prefixStmt = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class);
42 MissingSubstatementException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
43 "Prefix is mandatory substatement of import statement");
44 this.prefix = prefixStmt.get();
46 if (!ctx.isEnabledSemanticVersioning()) {
47 final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
48 RevisionDateEffectiveStatement.class);
49 this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
52 final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
53 ImportPrefixToSemVerSourceIdentifier.class, prefix);
54 revision = importedModuleIdentifier.getRevision().orElse(null);
55 semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
59 private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
61 * When 'revision-date' of an import is not specified in yang source, we
62 * need to find revision of imported module.
64 final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
65 SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
66 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
67 return importedModule.getRevision().orElse(null);
71 public String getModuleName() {
76 public Optional<Revision> getRevision() {
77 return Optional.ofNullable(revision);
81 public Optional<SemVer> getSemanticVersion() {
82 return Optional.ofNullable(semVer);
86 public String getPrefix() {
91 public int hashCode() {
92 return Objects.hash(moduleName, revision, prefix, semVer, nullableDescription(), nullableReference());
96 public boolean equals(final Object obj) {
103 if (getClass() != obj.getClass()) {
106 final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
107 return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
108 && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
109 && Objects.equals(nullableDescription(), other.nullableDescription())
110 && Objects.equals(nullableReference(), other.nullableReference());
114 public String toString() {
115 return MoreObjects.toStringHelper(this).omitNullValues().add("moduleName", getModuleName())
116 .add("revision", revision).add("version", semVer).add("prefix", getPrefix())
117 .add("description", nullableDescription()).add("reference", nullableReference()).toString();