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.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableMap.Builder;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Iterables;
18 import com.google.common.collect.Maps;
19 import java.util.Collection;
20 import java.util.HashSet;
22 import java.util.Map.Entry;
23 import java.util.Optional;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
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.BelongsToStatement;
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.MutableStatement;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
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, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
55 private ImmutableSet<Submodule> submodules;
56 private boolean sealed;
58 SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
59 final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
60 super(declared, ctx, substatements, findSubmodulePrefix(ctx));
62 final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
63 final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
66 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
67 appendPrefixes(ctx, prefixToModuleBuilder);
68 prefixToModule = prefixToModuleBuilder.build();
70 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
71 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
72 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
74 namespaceToPrefix = ImmutableMap.copyOf(tmp);
76 final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
77 RevisionEffectiveStatement.class);
78 this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
81 * Because of possible circular chains of includes between submodules we can
82 * collect only submodule contexts here and then build them during
83 * sealing of this statement.
85 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx.getAllFromCurrentStmtCtxNamespace(
86 IncludedSubmoduleNameToModuleCtx.class);
87 if (includedSubmodulesMap != null) {
88 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
89 submoduleContextsInit = new HashSet<>();
90 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
91 submoduleContextsInit.add(
92 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
94 submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
96 submoduleContexts = ImmutableSet.of();
99 if (!submoduleContexts.isEmpty()) {
100 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
103 submodules = ImmutableSet.of();
109 public QNameModule getQNameModule() {
114 @SuppressWarnings("unchecked")
115 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
116 final @NonNull Class<N> namespace) {
117 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
118 return Optional.of((Map<K, V>) prefixToModule);
120 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
121 return Optional.of((Map<K, V>) namespaceToPrefix);
123 return super.getNamespaceContents(namespace);
127 public Collection<? extends Submodule> getSubmodules() {
128 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
134 public SubmoduleEffectiveStatement asEffectiveStatement() {
141 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
142 ctx -> (Submodule) ctx.buildEffective()));
143 submoduleContexts = ImmutableSet.of();
148 private static @NonNull String findSubmodulePrefix(final StmtContext<String, ?, ?> ctx) {
149 final String name = ctx.getStatementArgument();
150 final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
151 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
152 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
153 return findPrefix(belongsTo, "submodule", name);