2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.import_;
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;
28 final class ImportEffectiveStatementImpl extends AbstractEffectiveDocumentedNodeWithoutStatus<String, ImportStatement>
29 implements ImportEffectiveStatement, ModuleImport {
31 private final String moduleName;
32 private final Revision revision;
33 private final SemVer semVer;
34 private final String prefix;
36 ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
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();
45 if (!ctx.isEnabledSemanticVersioning()) {
46 final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
47 RevisionDateEffectiveStatement.class);
48 this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
51 final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
52 ImportPrefixToSemVerSourceIdentifier.class, prefix);
53 revision = importedModuleIdentifier.getRevision().orElse(null);
54 semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
58 private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
60 * When 'revision-date' of an import is not specified in yang source, we
61 * need to find revision of imported module.
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);
70 public String getModuleName() {
75 public Optional<Revision> getRevision() {
76 return Optional.ofNullable(revision);
80 public Optional<SemVer> getSemanticVersion() {
81 return Optional.ofNullable(semVer);
85 public String getPrefix() {
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();