Fix isSupportedToBuildEffective() propagation
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.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.stmt.reactor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.YangVersion;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
44  * of this class, hence recursive lookups from them cross this class.
45  */
46 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
47         extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
48     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
49
50     private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
51     // These namespaces are well-known and not needed after the root is cleaned up
52     private static final Map<ParserNamespace<?, ?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
53         ParserNamespaces.GROUPING, new SweptNamespace(ParserNamespaces.GROUPING),
54         ParserNamespaces.schemaTree(), new SweptNamespace(ParserNamespaces.schemaTree()),
55         ParserNamespaces.TYPE, new SweptNamespace(ParserNamespaces.TYPE));
56
57     private final @NonNull SourceSpecificContext sourceContext;
58     private final A argument;
59
60     private YangVersion rootVersion;
61     private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
62     private SourceIdentifier rootIdentifier;
63
64     /**
65      * References to RootStatementContext of submodules which are included in this source.
66      */
67     private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
68
69     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
70             final StatementSourceReference ref, final String rawArgument) {
71         super(def, ref, rawArgument);
72         this.sourceContext = requireNonNull(sourceContext);
73         this.argument = def.parseArgumentValue(this, rawArgument());
74     }
75
76     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
77             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
78             final SourceIdentifier identifier) {
79         this(sourceContext, def, ref, rawArgument);
80         setRootVersion(version);
81         setRootIdentifier(identifier);
82     }
83
84     @Override
85     public StatementContextBase<?, ?, ?> getParentContext() {
86         // null as root cannot have parent
87         return null;
88     }
89
90     @Override
91     public NamespaceStorageNode getParentNamespaceStorage() {
92         // namespace storage of source context
93         return sourceContext;
94     }
95
96     @Override
97     public StorageNodeType getStorageNodeType() {
98         return StorageNodeType.ROOT_STATEMENT_LOCAL;
99     }
100
101     @Override
102     public RootStatementContext<?, ?, ?> getRoot() {
103         // this as its own root
104         return this;
105     }
106
107     SourceSpecificContext getSourceContext() {
108         return sourceContext;
109     }
110
111     @Override
112     public A argument() {
113         return argument;
114     }
115
116     @Override
117     public EffectiveConfig effectiveConfig() {
118         return EffectiveConfig.UNDETERMINED;
119     }
120
121     @Override
122     public <K, V> V putToLocalStorage(final ParserNamespace<K, V> type, final K key, final V value) {
123         if (ParserNamespaces.INCLUDED_MODULE.equals(type)) {
124             if (includedContexts.isEmpty()) {
125                 includedContexts = new ArrayList<>(1);
126             }
127             verify(value instanceof RootStatementContext);
128             includedContexts.add((RootStatementContext<?, ?, ?>) value);
129         }
130         return super.putToLocalStorage(type, key, value);
131     }
132
133     @Override
134     public <K, V> V getFromLocalStorage(final ParserNamespace<K, V> type, final K key) {
135         return getFromLocalStorage(type, key, new HashSet<>());
136     }
137
138     /*
139      * We need to track already checked RootStatementContexts due to possible
140      * circular chains of includes between submodules
141      */
142     private <K, V> @Nullable V getFromLocalStorage(final ParserNamespace<K, V> type, final K key,
143             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
144         final V potentialLocal = super.getFromLocalStorage(type, key);
145         if (potentialLocal != null) {
146             return potentialLocal;
147         }
148
149         alreadyChecked.add(this);
150         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
151             if (alreadyChecked.contains(includedSource)) {
152                 continue;
153             }
154             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
155             if (potential != null) {
156                 return potential;
157             }
158         }
159         return null;
160     }
161
162     @Override
163     public <K, V> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type) {
164         return getAllFromLocalStorage(type, new HashSet<>());
165     }
166
167     /*
168      * We need to track already checked RootStatementContexts due to possible
169      * circular chains of includes between submodules
170      */
171     private <K, V> @Nullable Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type,
172             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
173         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
174         if (potentialLocal != null) {
175             return potentialLocal;
176         }
177
178         alreadyChecked.add(this);
179         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
180             if (alreadyChecked.contains(includedSource)) {
181                 continue;
182             }
183             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
184             if (potential != null) {
185                 return potential;
186             }
187         }
188         return null;
189     }
190
191     /**
192      * Return the set of required sources.
193      *
194      * @return Required sources.
195      */
196     Collection<SourceIdentifier> getRequiredSources() {
197         return ImmutableSet.copyOf(requiredSources);
198     }
199
200     SourceIdentifier getRootIdentifier() {
201         return rootIdentifier;
202     }
203
204     @Override
205     protected boolean isIgnoringIfFeatures() {
206         return false;
207     }
208
209     @Override
210     protected boolean isIgnoringConfig() {
211         return false;
212     }
213
214     @Override
215     protected boolean isParentSupportedByFeatures() {
216         return true;
217     }
218
219     void setRootIdentifierImpl(final SourceIdentifier identifier) {
220         this.rootIdentifier = requireNonNull(identifier);
221     }
222
223     @NonNull Registry getBehaviourRegistryImpl() {
224         return sourceContext;
225     }
226
227     @NonNull YangVersion getRootVersionImpl() {
228         return rootVersion == null ? DEFAULT_VERSION : rootVersion;
229     }
230
231     void setRootVersionImpl(final YangVersion version) {
232         checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
233                 "Unsupported yang version %s in %s", version, sourceReference());
234         checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
235                 this.rootVersion);
236         this.rootVersion = requireNonNull(version);
237     }
238
239     /**
240      * Add mutable statement to seal. Each mutable statement must be sealed
241      * as the last step of statement parser processing.
242      *
243      * @param mutableStatement
244      *            mutable statement which should be sealed
245      */
246     void addMutableStmtToSeal(final MutableStatement mutableStatement) {
247         sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
248     }
249
250     void addRequiredSourceImpl(final SourceIdentifier dependency) {
251         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
252                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
253         if (requiredSources.isEmpty()) {
254             requiredSources = new HashSet<>();
255         }
256         requiredSources.add(dependency);
257     }
258
259     @Deprecated
260     @Override
261     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
262         throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
263     }
264
265     @Override
266     void sweepNamespaces() {
267         LOG.trace("Sweeping root {}", this);
268         sweepNamespaces(SWEPT_NAMESPACES);
269     }
270 }