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.submodule;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Maps;
18 import java.util.Collection;
19 import java.util.HashSet;
21 import java.util.Map.Entry;
22 import java.util.Optional;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.Revision;
27 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
28 import org.opendaylight.yangtools.yang.model.api.Submodule;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
31 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.PrefixToEffectiveModuleNamespace;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace;
35 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
44 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47 final class SubmoduleEffectiveStatementImpl
48 extends AbstractEffectiveModule<SubmoduleStatement, SubmoduleEffectiveStatement>
49 implements Submodule, SubmoduleEffectiveStatement, MutableStatement {
50 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
51 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
52 private final QNameModule qnameModule;
54 private Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContexts;
55 private ImmutableSet<Submodule> submodules;
56 private boolean sealed;
58 SubmoduleEffectiveStatementImpl(final Current<UnqualifiedQName, SubmoduleStatement> stmt,
59 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
60 super(stmt, substatements, findSubmodulePrefix(stmt, substatements));
62 final QNameModule belongsToModuleQName = stmt.getFromNamespace(ModuleNameToModuleQName.class,
63 findBelongsTo(stmt, substatements).argument());
65 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
66 appendPrefixes(stmt, prefixToModuleBuilder);
67 prefixToModule = prefixToModuleBuilder.build();
69 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
70 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
71 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
73 namespaceToPrefix = ImmutableMap.copyOf(tmp);
75 final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
76 RevisionEffectiveStatement.class);
77 this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
80 * Because of possible circular chains of includes between submodules we can
81 * collect only submodule contexts here and then build them during
82 * sealing of this statement.
84 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = stmt.localNamespacePortion(
85 IncludedSubmoduleNameToModuleCtx.class);
86 if (includedSubmodulesMap != null) {
87 final Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContextsInit =
89 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
90 submoduleContextsInit.add(
91 (StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>)submoduleCtx);
93 submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
95 submoduleContexts = ImmutableSet.of();
98 if (submoduleContexts.isEmpty()) {
99 submodules = ImmutableSet.of();
107 public QNameModule getQNameModule() {
112 @SuppressWarnings("unchecked")
113 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
114 final @NonNull Class<N> namespace) {
115 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
116 return Optional.of((Map<K, V>) prefixToModule);
118 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
119 return Optional.of((Map<K, V>) namespaceToPrefix);
121 return super.getNamespaceContents(namespace);
125 public Collection<? extends @NonNull Submodule> getSubmodules() {
126 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
132 public SubmoduleEffectiveStatement asEffectiveStatement() {
139 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
140 ctx -> (Submodule) ctx.buildEffective()));
141 submoduleContexts = ImmutableSet.of();
146 private static @NonNull BelongsToEffectiveStatement findBelongsTo(final CommonStmtCtx stmt,
147 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
148 return substatements.stream()
149 .filter(BelongsToEffectiveStatement.class::isInstance)
150 .map(BelongsToEffectiveStatement.class::cast)
151 .findAny().orElseThrow(() -> new SourceException(stmt,
152 "Unable to find belongs-to statement in submodule %s.", stmt.rawArgument()));
155 private static @NonNull String findSubmodulePrefix(final CommonStmtCtx stmt,
156 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
157 return findPrefix(stmt, findBelongsTo(stmt, substatements).effectiveSubstatements(), "submodule",
158 stmt.getRawArgument());