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.module;
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.concepts.SemVer;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
27 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
35 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
36 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
37 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
38 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
39 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
41 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
42 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
45 abstract class AbstractModuleStatementSupport
46 extends AbstractStatementSupport<String, ModuleStatement, ModuleEffectiveStatement> {
47 AbstractModuleStatementSupport() {
48 super(YangStmtMapping.MODULE);
52 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
57 public final ModuleStatement createDeclared(final StmtContext<String, ModuleStatement, ?> ctx) {
58 return new ModuleStatementImpl(ctx);
62 public final ModuleEffectiveStatement createEffective(
63 final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx) {
64 return new ModuleEffectiveStatementImpl(ctx);
68 public final void onPreLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
69 final String moduleName = stmt.getStatementArgument();
71 final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
72 SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
73 "Namespace of the module [%s] is missing", stmt.getStatementArgument());
74 stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
76 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
77 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
78 "Prefix of the module [%s] is missing", stmt.getStatementArgument());
79 stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
81 stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
83 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
84 final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
86 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
87 stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate));
91 public final void onLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
93 final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
94 NamespaceStatement.class));
95 SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
96 "Namespace of the module [%s] is missing", stmt.getStatementArgument());
98 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
99 final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
100 final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
101 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
102 if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
103 throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
104 qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
107 final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(),
110 stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
111 stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
112 stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
114 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
115 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
116 "Prefix of the module [%s] is missing", stmt.getStatementArgument());
118 stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
119 stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule);
120 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
121 stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
122 stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument());
123 stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
125 if (stmt.isEnabledSemanticVersioning()) {
126 addToSemVerModuleNamespace(stmt, moduleIdentifier);
130 private static void addToSemVerModuleNamespace(
131 final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt,
132 final SourceIdentifier moduleIdentifier) {
133 final String moduleName = stmt.coerceStatementArgument();
134 final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
135 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer);
136 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);