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.stmt.rfc6020.effective;
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.ImportStatement;
18 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
22 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
26 implements ModuleImport {
28 private final String moduleName;
29 private final Revision revision;
30 private final SemVer semVer;
31 private final String prefix;
32 private final String description;
33 private final String reference;
35 public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
38 moduleName = ctx.getStatementArgument();
39 final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
40 if (prefixStmt != null) {
41 this.prefix = prefixStmt.argument();
43 throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
44 ctx.getStatementSourceReference());
47 if (!ctx.isEnabledSemanticVersioning()) {
48 final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(
49 RevisionDateEffectiveStatementImpl.class);
50 this.revision = revisionDateStmt == null ? getImportedRevision(ctx) : revisionDateStmt.argument();
53 final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
54 ImportPrefixToSemVerSourceIdentifier.class, prefix);
55 revision = importedModuleIdentifier.getRevision().orElse(null);
56 semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
59 final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(
60 DescriptionEffectiveStatementImpl.class);
61 this.description = descriptionStmt != null ? descriptionStmt.argument() : null;
63 final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
64 this.reference = referenceStmt != null ? referenceStmt.argument() : null;
67 private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
69 * When 'revision-date' of an import is not specified in yang source, we
70 * need to find revision of imported module.
72 final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
73 SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
74 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
75 return importedModule.getRevision().orElse(null);
79 public String getModuleName() {
84 public Optional<Revision> getRevision() {
85 return Optional.ofNullable(revision);
89 public Optional<SemVer> getSemanticVersion() {
90 return Optional.ofNullable(semVer);
94 public String getPrefix() {
99 public String getDescription() {
104 public String getReference() {
109 public int hashCode() {
110 return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
114 public boolean equals(final Object obj) {
121 if (getClass() != obj.getClass()) {
124 final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
125 return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
126 && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
127 && Objects.equals(description, other.description) && Objects.equals(reference, other.reference);
131 public String toString() {
132 return MoreObjects.toStringHelper(this).add("moduleName", getModuleName())
133 .add("revision", getRevision()).add("semantic version", getSemanticVersion())
134 .add("prefix", getPrefix()).add("description", getDescription())
135 .add("reference", getReference()).toString();