Refactor {Module,Submodule}EffectiveStatementImpl
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / submodule / SubmoduleEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.submodule;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
12
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;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.Set;
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;
45
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;
52
53     private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
54     private ImmutableSet<Module> submodules;
55     private boolean sealed;
56
57     SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
58             final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
59         super(declared, ctx, substatements, findSubmodulePrefix(ctx));
60
61         final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
62         final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
63                 belongsToModuleName);
64
65         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
66         appendPrefixes(ctx, prefixToModuleBuilder);
67         prefixToModule = prefixToModuleBuilder.build();
68
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());
72         }
73         namespaceToPrefix = ImmutableMap.copyOf(tmp);
74
75         final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
76             RevisionEffectiveStatement.class);
77         this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
78
79         /*
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.
83          */
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);
92             }
93             submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
94         } else {
95             submoduleContexts = ImmutableSet.of();
96         }
97
98         if (!submoduleContexts.isEmpty()) {
99             ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
100             sealed = false;
101         } else {
102             submodules = ImmutableSet.of();
103             sealed = true;
104         }
105     }
106
107     @Override
108     public QNameModule getQNameModule() {
109         return qnameModule;
110     }
111
112     @Override
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);
118         }
119         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
120             return Optional.of((Map<K, V>) namespaceToPrefix);
121         }
122         return super.getNamespaceContents(namespace);
123     }
124
125     @Override
126     public Set<Module> getSubmodules() {
127         checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
128             qnameModule);
129         return submodules;
130     }
131
132     @Override
133     public void seal() {
134         if (!sealed) {
135             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
136                 ctx -> (Module) ctx.buildEffective()));
137             submoduleContexts = ImmutableSet.of();
138             sealed = true;
139         }
140     }
141
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);
148     }
149 }