Remove hashCode()/equals() from SchemaNode implementations
[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.Optional;
12 import org.opendaylight.yangtools.concepts.SemVer;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.common.Revision;
15 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedNodeWithoutStatus;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 final class ImportEffectiveStatementImpl extends AbstractEffectiveDocumentedNodeWithoutStatus<String, ImportStatement>
29         implements ImportEffectiveStatement, ModuleImport {
30
31     private final String moduleName;
32     private final Revision revision;
33     private final SemVer semVer;
34     private final String prefix;
35
36     ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
37         super(ctx);
38
39         moduleName = ctx.coerceStatementArgument();
40         final Optional<String> prefixStmt = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class);
41         MissingSubstatementException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
42             "Prefix is mandatory substatement of import statement");
43         this.prefix = prefixStmt.get();
44
45         if (!ctx.isEnabledSemanticVersioning()) {
46             final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
47                 RevisionDateEffectiveStatement.class);
48             this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
49             this.semVer = null;
50         } else {
51             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
52                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
53             revision = importedModuleIdentifier.getRevision().orElse(null);
54             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
55         }
56     }
57
58     private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
59         /*
60          * When 'revision-date' of an import is not specified in yang source, we
61          * need to find revision of imported module.
62          */
63         final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
64         SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
65                 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
66         return importedModule.getRevision().orElse(null);
67     }
68
69     @Override
70     public String getModuleName() {
71         return moduleName;
72     }
73
74     @Override
75     public Optional<Revision> getRevision() {
76         return Optional.ofNullable(revision);
77     }
78
79     @Override
80     public Optional<SemVer> getSemanticVersion() {
81         return Optional.ofNullable(semVer);
82     }
83
84     @Override
85     public String getPrefix() {
86         return prefix;
87     }
88
89     @Override
90     public String toString() {
91         return MoreObjects.toStringHelper(this).omitNullValues().add("moduleName", getModuleName())
92                 .add("revision", revision).add("version", semVer).add("prefix", getPrefix())
93                 .add("description", nullableDescription()).add("reference", nullableReference()).toString();
94     }
95 }