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.meta.EffectiveStatement;
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 extends
46 AbstractStatementSupport<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> {
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 EffectiveStatement<String, ModuleStatement> createEffective(
63 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
64 return new ModuleEffectiveStatementImpl(ctx);
68 public final void onPreLinkageDeclared(final Mutable<String, ModuleStatement,
69 EffectiveStatement<String, ModuleStatement>> stmt) {
70 final String moduleName = stmt.getStatementArgument();
72 final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
73 SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
74 "Namespace of the module [%s] is missing", stmt.getStatementArgument());
75 stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
77 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
78 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
79 "Prefix of the module [%s] is missing", stmt.getStatementArgument());
80 stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
82 stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
84 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
85 final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
87 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
88 stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate));
92 public final void onLinkageDeclared(final Mutable<String, ModuleStatement,
93 EffectiveStatement<String, ModuleStatement>> stmt) {
95 final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
96 NamespaceStatement.class));
97 SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
98 "Namespace of the module [%s] is missing", stmt.getStatementArgument());
100 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
101 final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
102 final StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>> possibleDuplicateModule =
103 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
104 if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
105 throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
106 qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
109 final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(),
112 stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
113 stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
114 stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
116 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
117 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
118 "Prefix of the module [%s] is missing", stmt.getStatementArgument());
120 stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
121 stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule);
122 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
123 stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
124 stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument());
125 stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
127 if (stmt.isEnabledSemanticVersioning()) {
128 addToSemVerModuleNamespace(stmt, moduleIdentifier);
132 private static void addToSemVerModuleNamespace(
133 final Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt,
134 final SourceIdentifier moduleIdentifier) {
135 final String moduleName = stmt.coerceStatementArgument();
136 final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
137 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer);
138 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);