YANGTOOLS-706: Split out yang-parser-rfc7950
[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.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;
30
31 final class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
32         implements ImportEffectiveStatement, ModuleImport {
33
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;
40
41     ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
42         super(ctx);
43
44         moduleName = ctx.getStatementArgument();
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();
49
50         if (!ctx.isEnabledSemanticVersioning()) {
51             final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
52                 RevisionDateEffectiveStatement.class);
53             this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
54             this.semVer = null;
55         } else {
56             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
57                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
58             revision = importedModuleIdentifier.getRevision().orElse(null);
59             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
60         }
61
62         description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
63         reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
64     }
65
66     private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
67         /*
68          * When 'revision-date' of an import is not specified in yang source, we
69          * need to find revision of imported module.
70          */
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);
75     }
76
77     @Override
78     public String getModuleName() {
79         return moduleName;
80     }
81
82     @Override
83     public Optional<Revision> getRevision() {
84         return Optional.ofNullable(revision);
85     }
86
87     @Override
88     public Optional<SemVer> getSemanticVersion() {
89         return Optional.ofNullable(semVer);
90     }
91
92     @Override
93     public String getPrefix() {
94         return prefix;
95     }
96
97     @Override
98     public Optional<String> getDescription() {
99         return Optional.ofNullable(description);
100     }
101
102     @Override
103     public Optional<String> getReference() {
104         return Optional.ofNullable(reference);
105     }
106
107     @Override
108     public int hashCode() {
109         return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
110     }
111
112     @Override
113     public boolean equals(final Object obj) {
114         if (this == obj) {
115             return true;
116         }
117         if (obj == null) {
118             return false;
119         }
120         if (getClass() != obj.getClass()) {
121             return false;
122         }
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);
127     }
128
129     @Override
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();
134     }
135 }