Migrate users of deprecated QNameModule methods
[yangtools.git] / parser / 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
12 import com.google.common.collect.ImmutableList;
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.Collection;
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.common.UnresolvedQName.Unqualified;
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.stmt.BelongsToEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
36 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
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.source.SourceException;
42
43 final class SubmoduleEffectiveStatementImpl
44         extends AbstractEffectiveModule<SubmoduleStatement, SubmoduleEffectiveStatement>
45         implements Submodule, SubmoduleEffectiveStatement, MutableStatement {
46     private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
47     private final ImmutableMap<QNameModule, String> namespaceToPrefix;
48     private final QNameModule qnameModule;
49
50     private Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContexts;
51     private ImmutableSet<Submodule> submodules;
52     private boolean sealed;
53
54     SubmoduleEffectiveStatementImpl(final Current<Unqualified, SubmoduleStatement> stmt,
55             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
56         super(stmt, substatements, findSubmodulePrefix(stmt, substatements));
57
58         final QNameModule belongsToModuleQName = stmt.namespaceItem(ParserNamespaces.MODULE_NAME_TO_QNAME,
59             findBelongsTo(stmt, substatements).argument());
60
61         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
62         appendPrefixes(stmt, prefixToModuleBuilder);
63         prefixToModule = prefixToModuleBuilder.build();
64
65         final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
66         for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
67             tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
68         }
69         namespaceToPrefix = ImmutableMap.copyOf(tmp);
70
71         final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
72             RevisionEffectiveStatement.class);
73         qnameModule = QNameModule.ofRevision(belongsToModuleQName.namespace(), submoduleRevision.orElse(null))
74             .intern();
75
76         /*
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.
80          */
81         final Map<Unqualified, StmtContext<?, ?, ?>> includedSubmodulesMap = stmt.localNamespacePortion(
82             ParserNamespaces.INCLUDED_SUBMODULE_NAME_TO_MODULECTX);
83         if (includedSubmodulesMap != null) {
84             final Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContextsInit =
85                 new HashSet<>();
86             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
87                 submoduleContextsInit.add(
88                     (StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>)submoduleCtx);
89             }
90             submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
91         } else {
92             submoduleContexts = ImmutableSet.of();
93         }
94
95         if (submoduleContexts.isEmpty()) {
96             submodules = ImmutableSet.of();
97             sealed = true;
98         } else {
99             sealed = false;
100         }
101     }
102
103     @Override
104     public QNameModule getQNameModule() {
105         return qnameModule;
106     }
107
108     @Override
109     public Collection<Entry<String, ModuleEffectiveStatement>> reachableModules() {
110         return prefixToModule.entrySet();
111     }
112
113     @Override
114     public Optional<ModuleEffectiveStatement> findReachableModule(final String prefix) {
115         return findValue(prefixToModule, prefix);
116     }
117
118     @Override
119     public Collection<Entry<QNameModule, String>> namespacePrefixes() {
120         return namespaceToPrefix.entrySet();
121     }
122
123     @Override
124     public Optional<String> findNamespacePrefix(final QNameModule namespace) {
125         return findValue(namespaceToPrefix, namespace);
126     }
127
128     @Override
129     public Collection<? extends @NonNull Submodule> getSubmodules() {
130         checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
131             qnameModule);
132         return submodules;
133     }
134
135     @Override
136     public SubmoduleEffectiveStatement asEffectiveStatement() {
137         return this;
138     }
139
140     @Override
141     public void seal() {
142         if (!sealed) {
143             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
144                 ctx -> (Submodule) ctx.buildEffective()));
145             submoduleContexts = ImmutableSet.of();
146             sealed = true;
147         }
148     }
149
150     private static @NonNull BelongsToEffectiveStatement findBelongsTo(final CommonStmtCtx stmt,
151             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
152         return substatements.stream()
153             .filter(BelongsToEffectiveStatement.class::isInstance)
154             .map(BelongsToEffectiveStatement.class::cast)
155             .findAny().orElseThrow(() -> new SourceException(stmt,
156                 "Unable to find belongs-to statement in submodule %s.", stmt.rawArgument()));
157     }
158
159     private static @NonNull String findSubmodulePrefix(final CommonStmtCtx stmt,
160             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
161         return findPrefix(stmt, findBelongsTo(stmt, substatements).effectiveSubstatements(), "submodule",
162             stmt.getRawArgument());
163     }
164 }