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.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.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
30 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.PrefixToEffectiveModuleNamespace;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace;
34 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
42 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 final class SubmoduleEffectiveStatementImpl
47 extends AbstractEffectiveModule<SubmoduleStatement, SubmoduleEffectiveStatement>
48 implements SubmoduleEffectiveStatement, MutableStatement {
49 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
50 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
51 private final QNameModule qnameModule;
53 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
54 private ImmutableSet<Module> submodules;
55 private boolean sealed;
57 SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
58 final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
59 super(declared, ctx, substatements, findSubmodulePrefix(ctx));
61 final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
62 final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
65 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
66 appendPrefixes(ctx, 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 = ctx.getAllFromCurrentStmtCtxNamespace(
85 IncludedSubmoduleNameToModuleCtx.class);
86 if (includedSubmodulesMap != null) {
87 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
88 submoduleContextsInit = new HashSet<>();
89 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
90 submoduleContextsInit.add(
91 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
93 submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
95 submoduleContexts = ImmutableSet.of();
98 if (!submoduleContexts.isEmpty()) {
99 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
102 submodules = ImmutableSet.of();
108 public QNameModule getQNameModule() {
113 @SuppressWarnings("unchecked")
114 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
115 final @NonNull Class<N> namespace) {
116 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
117 return Optional.of((Map<K, V>) prefixToModule);
119 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
120 return Optional.of((Map<K, V>) namespaceToPrefix);
122 return super.getNamespaceContents(namespace);
126 public Set<Module> getSubmodules() {
127 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
135 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
136 ctx -> (Module) ctx.buildEffective()));
137 submoduleContexts = ImmutableSet.of();
142 private static @NonNull String findSubmodulePrefix(final StmtContext<String, ?, ?> ctx) {
143 final String name = ctx.getStatementArgument();
144 final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
145 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
146 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
147 return findPrefix(belongsTo, "submodule", name);