09d0ce8e674b43d367b6760a79d8d8690b69df16
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / AbstractModuleStatementSupport.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.module;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
11
12 import java.net.URI;
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;
44
45 abstract class AbstractModuleStatementSupport
46         extends AbstractStatementSupport<String, ModuleStatement, ModuleEffectiveStatement> {
47     AbstractModuleStatementSupport() {
48         super(YangStmtMapping.MODULE);
49     }
50
51     @Override
52     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53         return value;
54     }
55
56     @Override
57     public final ModuleStatement createDeclared(final StmtContext<String, ModuleStatement, ?> ctx) {
58         return new ModuleStatementImpl(ctx);
59     }
60
61     @Override
62     public final ModuleEffectiveStatement createEffective(
63             final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx) {
64         return new ModuleEffectiveStatementImpl(ctx);
65     }
66
67     @Override
68     public final void onPreLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
69         final String moduleName = stmt.getStatementArgument();
70
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);
75
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);
80
81         stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
82
83         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
84         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
85
86         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
87         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate));
88     }
89
90     @Override
91     public final void onLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
92
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());
97
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());
105         }
106
107         final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(),
108                 revisionDate);
109
110         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
111         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
112         stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
113
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());
117
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);
124
125         if (stmt.isEnabledSemanticVersioning()) {
126             addToSemVerModuleNamespace(stmt, moduleIdentifier);
127         }
128     }
129
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);
137     }
138 }