d3feaad3de45f35256c166f685e518b01e6e558b
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / 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.stmt.rfc6020.effective;
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.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;
24
25 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
26         implements ModuleImport {
27
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;
34
35     public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
36         super(ctx);
37
38         moduleName = ctx.getStatementArgument();
39         final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
40         if (prefixStmt != null) {
41             this.prefix = prefixStmt.argument();
42         } else {
43             throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
44                     ctx.getStatementSourceReference());
45         }
46
47         if (!ctx.isEnabledSemanticVersioning()) {
48             final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(
49                 RevisionDateEffectiveStatementImpl.class);
50             this.revision = revisionDateStmt == null ? getImportedRevision(ctx) : revisionDateStmt.argument();
51             this.semVer = null;
52         } else {
53             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
54                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
55             revision = importedModuleIdentifier.getRevision().orElse(null);
56             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
57         }
58
59         final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(
60             DescriptionEffectiveStatementImpl.class);
61         this.description = descriptionStmt != null ? descriptionStmt.argument() : null;
62
63         final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
64         this.reference = referenceStmt != null ? referenceStmt.argument() : null;
65     }
66
67     private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
68         /*
69          * When 'revision-date' of an import is not specified in yang source, we
70          * need to find revision of imported module.
71          */
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);
76     }
77
78     @Override
79     public String getModuleName() {
80         return moduleName;
81     }
82
83     @Override
84     public Optional<Revision> getRevision() {
85         return Optional.ofNullable(revision);
86     }
87
88     @Override
89     public Optional<SemVer> getSemanticVersion() {
90         return Optional.ofNullable(semVer);
91     }
92
93     @Override
94     public String getPrefix() {
95         return prefix;
96     }
97
98     @Override
99     public Optional<String> getDescription() {
100         return Optional.ofNullable(description);
101     }
102
103     @Override
104     public Optional<String> getReference() {
105         return Optional.ofNullable(reference);
106     }
107
108     @Override
109     public int hashCode() {
110         return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
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);
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).omitNullValues().add("moduleName", getModuleName())
133                 .add("revision", revision).add("version", semVer).add("prefix", getPrefix())
134                 .add("description", description).add("reference", reference).toString();
135     }
136 }