Remove hashCode()/equals() from SchemaNode implementations
[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 extends AbstractEffectiveModule<SubmoduleStatement>
46         implements SubmoduleEffectiveStatement, MutableStatement {
47     private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
48     private final ImmutableMap<QNameModule, String> namespaceToPrefix;
49     private final QNameModule qnameModule;
50
51     private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContexts;
52     private ImmutableSet<Module> submodules;
53     private boolean sealed;
54
55     SubmoduleEffectiveStatementImpl(final StmtContext<String, SubmoduleStatement, SubmoduleEffectiveStatement> ctx) {
56         super(ctx, findSubmodulePrefix(ctx));
57
58         final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
59         final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
60                 belongsToModuleName);
61
62         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
63         appendPrefixes(ctx, prefixToModuleBuilder);
64         prefixToModule = prefixToModuleBuilder.build();
65
66         final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size());
67         for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
68             tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
69         }
70         namespaceToPrefix = ImmutableMap.copyOf(tmp);
71
72         final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
73             RevisionEffectiveStatement.class);
74         this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).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<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx.getAllFromCurrentStmtCtxNamespace(
82             IncludedSubmoduleNameToModuleCtx.class);
83         if (includedSubmodulesMap != null) {
84             final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
85                 submoduleContextsInit = new HashSet<>();
86             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
87                 submoduleContextsInit.add(
88                     (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
89             }
90             submoduleContexts = ImmutableSet.copyOf(submoduleContextsInit);
91         } else {
92             submoduleContexts = ImmutableSet.of();
93         }
94
95         if (!submoduleContexts.isEmpty()) {
96             ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
97             sealed = false;
98         } else {
99             submodules = ImmutableSet.of();
100             sealed = true;
101         }
102     }
103
104     @Override
105     public QNameModule getQNameModule() {
106         return qnameModule;
107     }
108
109     @Override
110     @SuppressWarnings("unchecked")
111     public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
112             final @NonNull Class<N> namespace) {
113         if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
114             return Optional.of((Map<K, V>) prefixToModule);
115         }
116         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
117             return Optional.of((Map<K, V>) namespaceToPrefix);
118         }
119         return super.getNamespaceContents(namespace);
120     }
121
122     @Override
123     public Set<Module> getSubmodules() {
124         checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
125             qnameModule);
126         return submodules;
127     }
128
129     @Override
130     public void seal() {
131         if (!sealed) {
132             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContexts,
133                 ctx -> (Module) ctx.buildEffective()));
134             submoduleContexts = ImmutableSet.of();
135             sealed = true;
136         }
137     }
138
139     private static @NonNull String findSubmodulePrefix(final StmtContext<String, ?, ?> ctx) {
140         final String name = ctx.getStatementArgument();
141         final StmtContext<?, ?, ?> belongsTo = SourceException.throwIfNull(
142                 StmtContextUtils.findFirstDeclaredSubstatement(ctx, BelongsToStatement.class),
143                 ctx.getStatementSourceReference(), "Unable to find belongs-to statement in submodule %s.", name);
144         return findPrefix(belongsTo, "submodule", name);
145     }
146 }