Add AbstractEffectiveDocumentedNodeWithoutStatus
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / import_ / ImportEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.import_;
9
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;
28
29 final class ImportEffectiveStatementImpl extends AbstractEffectiveDocumentedNodeWithoutStatus<String, ImportStatement>
30         implements ImportEffectiveStatement, ModuleImport {
31
32     private final String moduleName;
33     private final Revision revision;
34     private final SemVer semVer;
35     private final String prefix;
36
37     ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
38         super(ctx);
39
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();
45
46         if (!ctx.isEnabledSemanticVersioning()) {
47             final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
48                 RevisionDateEffectiveStatement.class);
49             this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
50             this.semVer = null;
51         } else {
52             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
53                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
54             revision = importedModuleIdentifier.getRevision().orElse(null);
55             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
56         }
57     }
58
59     private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
60         /*
61          * When 'revision-date' of an import is not specified in yang source, we
62          * need to find revision of imported module.
63          */
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);
68     }
69
70     @Override
71     public String getModuleName() {
72         return moduleName;
73     }
74
75     @Override
76     public Optional<Revision> getRevision() {
77         return Optional.ofNullable(revision);
78     }
79
80     @Override
81     public Optional<SemVer> getSemanticVersion() {
82         return Optional.ofNullable(semVer);
83     }
84
85     @Override
86     public String getPrefix() {
87         return prefix;
88     }
89
90     @Override
91     public int hashCode() {
92         return Objects.hash(moduleName, revision, prefix, semVer, nullableDescription(), nullableReference());
93     }
94
95     @Override
96     public boolean equals(final Object obj) {
97         if (this == obj) {
98             return true;
99         }
100         if (obj == null) {
101             return false;
102         }
103         if (getClass() != obj.getClass()) {
104             return false;
105         }
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());
111     }
112
113     @Override
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();
118     }
119 }