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