Populate xpath/ hierarchy
[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
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.UnqualifiedQName;
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.BelongsToEffectiveStatement;
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.CommonStmtCtx;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
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;
46
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;
53
54     private Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContexts;
55     private ImmutableSet<Submodule> submodules;
56     private boolean sealed;
57
58     SubmoduleEffectiveStatementImpl(final Current<UnqualifiedQName, SubmoduleStatement> stmt,
59             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
60         super(stmt, substatements, findSubmodulePrefix(stmt, substatements));
61
62         final QNameModule belongsToModuleQName = stmt.getFromNamespace(ModuleNameToModuleQName.class,
63             findBelongsTo(stmt, substatements).argument());
64
65         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
66         appendPrefixes(stmt, 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 = stmt.localNamespacePortion(
85             IncludedSubmoduleNameToModuleCtx.class);
86         if (includedSubmodulesMap != null) {
87             final Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContextsInit =
88                 new HashSet<>();
89             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
90                 submoduleContextsInit.add(
91                     (StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>)submoduleCtx);
92             }
93             submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
94         } else {
95             submoduleContexts = ImmutableSet.of();
96         }
97
98         if (submoduleContexts.isEmpty()) {
99             submodules = ImmutableSet.of();
100             sealed = true;
101         } else {
102             sealed = false;
103         }
104     }
105
106     @Override
107     public QNameModule getQNameModule() {
108         return qnameModule;
109     }
110
111     @Override
112     @SuppressWarnings("unchecked")
113     public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
114             final @NonNull Class<N> namespace) {
115         if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
116             return Optional.of((Map<K, V>) prefixToModule);
117         }
118         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
119             return Optional.of((Map<K, V>) namespaceToPrefix);
120         }
121         return super.getNamespaceContents(namespace);
122     }
123
124     @Override
125     public Collection<? extends @NonNull Submodule> getSubmodules() {
126         checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
127             qnameModule);
128         return submodules;
129     }
130
131     @Override
132     public SubmoduleEffectiveStatement asEffectiveStatement() {
133         return this;
134     }
135
136     @Override
137     public void seal() {
138         if (!sealed) {
139             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
140                 ctx -> (Submodule) ctx.buildEffective()));
141             submoduleContexts = ImmutableSet.of();
142             sealed = true;
143         }
144     }
145
146     private static @NonNull BelongsToEffectiveStatement findBelongsTo(final CommonStmtCtx stmt,
147             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
148         return substatements.stream()
149             .filter(BelongsToEffectiveStatement.class::isInstance)
150             .map(BelongsToEffectiveStatement.class::cast)
151             .findAny().orElseThrow(() -> new SourceException(stmt,
152                 "Unable to find belongs-to statement in submodule %s.", stmt.rawArgument()));
153     }
154
155     private static @NonNull String findSubmodulePrefix(final CommonStmtCtx stmt,
156             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
157         return findPrefix(stmt, findBelongsTo(stmt, substatements).effectiveSubstatements(), "submodule",
158             stmt.getRawArgument());
159     }
160 }