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
46 extends AbstractEffectiveModule<SubmoduleStatement, SubmoduleEffectiveStatement>
47 implements SubmoduleEffectiveStatement, MutableStatement {
48 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
49 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
50 private final QNameModule qnameModule;
52 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
53 private ImmutableSet<Module> submodules;
54 private boolean sealed;
56 SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx) {
57 super(ctx, findSubmodulePrefix(ctx));
59 final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
60 final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
63 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
64 appendPrefixes(ctx, prefixToModuleBuilder);
65 prefixToModule = prefixToModuleBuilder.build();
67 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
68 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
69 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
71 namespaceToPrefix = ImmutableMap.copyOf(tmp);
73 final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
74 RevisionEffectiveStatement.class);
75 this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
78 * Because of possible circular chains of includes between submodules we can
79 * collect only submodule contexts here and then build them during
80 * sealing of this statement.
82 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx.getAllFromCurrentStmtCtxNamespace(
83 IncludedSubmoduleNameToModuleCtx.class);
84 if (includedSubmodulesMap != null) {
85 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
86 submoduleContextsInit = new HashSet<>();
87 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
88 submoduleContextsInit.add(
89 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
91 submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
93 submoduleContexts = ImmutableSet.of();
96 if (!submoduleContexts.isEmpty()) {
97 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
100 submodules = ImmutableSet.of();
106 public QNameModule getQNameModule() {
111 @SuppressWarnings("unchecked")
112 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
113 final @NonNull Class<N> namespace) {
114 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
115 return Optional.of((Map<K, V>) prefixToModule);
117 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
118 return Optional.of((Map<K, V>) namespaceToPrefix);
120 return super.getNamespaceContents(namespace);
124 public Set<Module> getSubmodules() {
125 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
133 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
134 ctx -> (Module) ctx.buildEffective()));
135 submoduleContexts = ImmutableSet.of();
140 private static @NonNull String findSubmodulePrefix(final StmtContext<String, ?, ?> ctx) {
141 final String name = ctx.getStatementArgument();
142 final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
143 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
144 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
145 return findPrefix(belongsTo, "submodule", name);