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;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
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.HashSet;
20 import java.util.Map.Entry;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.PrefixToEffectiveModuleNamespace;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace;
33 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
41 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
42 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
45 final class SubmoduleEffectiveStatementImpl extends AbstractEffectiveModule<SubmoduleStatement>
46 implements SubmoduleEffectiveStatement, MutableStatement {
47 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
48 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
49 private final QNameModule qnameModule;
51 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
52 private ImmutableSet<Module> submodules;
53 private boolean sealed;
55 SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx) {
56 super(ctx, findSubmodulePrefix(ctx));
58 final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
59 final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
62 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
63 appendPrefixes(ctx, prefixToModuleBuilder);
64 prefixToModule = prefixToModuleBuilder.build();
66 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
67 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
68 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
70 namespaceToPrefix = ImmutableMap.copyOf(tmp);
72 final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
73 RevisionEffectiveStatement.class);
74 this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
77 * Because of possible circular chains of includes between submodules we can
78 * collect only submodule contexts here and then build them during
79 * sealing of this statement.
81 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx.getAllFromCurrentStmtCtxNamespace(
82 IncludedSubmoduleNameToModuleCtx.class);
83 if (includedSubmodulesMap != null) {
84 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
85 submoduleContextsInit = new HashSet<>();
86 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
87 submoduleContextsInit.add(
88 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
90 submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
92 submoduleContexts = ImmutableSet.of();
95 if (!submoduleContexts.isEmpty()) {
96 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
99 submodules = ImmutableSet.of();
105 public QNameModule getQNameModule() {
110 @SuppressWarnings("unchecked")
111 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
112 final @NonNull Class<N> namespace) {
113 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
114 return Optional.of((Map<K, V>) prefixToModule);
116 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
117 return Optional.of((Map<K, V>) namespaceToPrefix);
119 return super.getNamespaceContents(namespace);
123 public Set<Module> getSubmodules() {
124 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
132 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
133 ctx -> (Module) ctx.buildEffective()));
134 submoduleContexts = ImmutableSet.of();
139 private static @NonNull String findSubmodulePrefix(final StmtContext<String, ?, ?> ctx) {
140 final String name = ctx.getStatementArgument();
141 final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
142 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
143 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
144 return findPrefix(belongsTo, "submodule", name);