d8d512bcd88c842298519cee9133aa9880c9fe11
[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.Collection;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Optional;
24 import java.util.Set;
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.common.UnqualifiedQName;
29 import org.opendaylight.yangtools.yang.model.api.Submodule;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
32 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.PrefixToEffectiveModuleNamespace;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace;
36 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
44 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
45 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
46 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47
48 final class SubmoduleEffectiveStatementImpl
49         extends AbstractEffectiveModule<SubmoduleStatement, SubmoduleEffectiveStatement>
50         implements Submodule, SubmoduleEffectiveStatement, MutableStatement {
51     private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
52     private final ImmutableMap<QNameModule, String> namespaceToPrefix;
53     private final QNameModule qnameModule;
54
55     private Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContexts;
56     private ImmutableSet<Submodule> submodules;
57     private boolean sealed;
58
59     SubmoduleEffectiveStatementImpl(
60         final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
61             final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62         super(declared, ctx, substatements, findSubmodulePrefix(ctx));
63
64         final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
65         final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
66                 belongsToModuleName);
67
68         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
69         appendPrefixes(ctx, prefixToModuleBuilder);
70         prefixToModule = prefixToModuleBuilder.build();
71
72         final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
73         for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
74             tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
75         }
76         namespaceToPrefix = ImmutableMap.copyOf(tmp);
77
78         final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
79             RevisionEffectiveStatement.class);
80         this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
81
82         /*
83          * Because of possible circular chains of includes between submodules we can
84          * collect only submodule contexts here and then build them during
85          * sealing of this statement.
86          */
87         final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx.getAllFromCurrentStmtCtxNamespace(
88             IncludedSubmoduleNameToModuleCtx.class);
89         if (includedSubmodulesMap != null) {
90             final Set<StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>> submoduleContextsInit =
91                 new HashSet<>();
92             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
93                 submoduleContextsInit.add(
94                     (StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>)submoduleCtx);
95             }
96             submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
97         } else {
98             submoduleContexts = ImmutableSet.of();
99         }
100
101         if (!submoduleContexts.isEmpty()) {
102             ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
103             sealed = false;
104         } else {
105             submodules = ImmutableSet.of();
106             sealed = true;
107         }
108     }
109
110     @Override
111     public QNameModule getQNameModule() {
112         return qnameModule;
113     }
114
115     @Override
116     @SuppressWarnings("unchecked")
117     public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
118             final @NonNull Class<N> namespace) {
119         if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
120             return Optional.of((Map<K, V>) prefixToModule);
121         }
122         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
123             return Optional.of((Map<K, V>) namespaceToPrefix);
124         }
125         return super.getNamespaceContents(namespace);
126     }
127
128     @Override
129     public Collection<? extends 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 String findSubmodulePrefix(final StmtContext<UnqualifiedQName, ?, ?> ctx) {
151         final String name = ctx.coerceRawStatementArgument();
152         final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
153                 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
154                 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
155         return findPrefix(belongsTo, "submodule", name);
156     }
157 }