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