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.Date;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
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.ImpPrefixToSemVerModuleIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
28 private final String moduleName;
29 private final Date 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(RevisionDateEffectiveStatementImpl.class);
49 this.revision = (revisionDateStmt == null) ? getImportedRevision(ctx) : revisionDateStmt
51 this.semVer = Module.DEFAULT_SEMANTIC_VERSION;
53 final ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToSemVerModuleIdentifier.class, prefix);
54 revision = importedModuleIdentifier.getRevision();
55 semVer = importedModuleIdentifier.getSemanticVersion();
58 final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
59 this.description = (descriptionStmt != null) ? descriptionStmt.argument() : null;
61 final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
62 this.reference = (referenceStmt != null) ? referenceStmt.argument() : null;
65 private Date getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
67 * When 'revision-date' of an import is not specified in yang source, we
68 * need to find revision of imported module.
70 final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
71 SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
72 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
73 return importedModule.getRevision();
77 public String getModuleName() {
82 public Date getRevision() {
87 public SemVer getSemanticVersion() {
92 public String getPrefix() {
97 public String getDescription() {
102 public String getReference() {
107 public int hashCode() {
108 return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
112 public boolean equals(final Object obj) {
119 if (getClass() != obj.getClass()) {
122 final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
123 return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
124 && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
125 && Objects.equals(description, other.description) && Objects.equals(reference, other.reference);
129 public String toString() {
130 return MoreObjects.toStringHelper(this).add("moduleName", getModuleName())
131 .add("revision", getRevision()).add("semantic version", getSemanticVersion())
132 .add("prefix", getPrefix()).add("description", getDescription())
133 .add("reference", getReference()).toString();