BUG-7052: Move qnameFromArgument to StmtContextUtils
[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.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;
24
25 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
26         ModuleImport {
27
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;
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(RevisionDateEffectiveStatementImpl.class);
49             this.revision = (revisionDateStmt == null) ? getImportedRevision(ctx) : revisionDateStmt
50                     .argument();
51             this.semVer = Module.DEFAULT_SEMANTIC_VERSION;
52         } else {
53             final ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToSemVerModuleIdentifier.class, prefix);
54             revision = importedModuleIdentifier.getRevision();
55             semVer = importedModuleIdentifier.getSemanticVersion();
56         }
57
58         final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
59         this.description = (descriptionStmt != null) ? descriptionStmt.argument() : null;
60
61         final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
62         this.reference = (referenceStmt != null) ? referenceStmt.argument() : null;
63     }
64
65     private Date getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
66         /*
67          * When 'revision-date' of an import is not specified in yang source, we
68          * need to find revision of imported module.
69          */
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();
74     }
75
76     @Override
77     public String getModuleName() {
78         return moduleName;
79     }
80
81     @Override
82     public Date getRevision() {
83         return revision;
84     }
85
86     @Override
87     public SemVer getSemanticVersion() {
88         return semVer;
89     }
90
91     @Override
92     public String getPrefix() {
93         return prefix;
94     }
95
96     @Override
97     public String getDescription() {
98         return description;
99     }
100
101     @Override
102     public String getReference() {
103         return reference;
104     }
105
106     @Override
107     public int hashCode() {
108         return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
109     }
110
111     @Override
112     public boolean equals(final Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (obj == null) {
117             return false;
118         }
119         if (getClass() != obj.getClass()) {
120             return false;
121         }
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);
126     }
127
128     @Override
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();
134     }
135 }