Perform partial substatement initialization
[yangtools.git] / yang / 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.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
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.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
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.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
40
41 /**
42  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
43  * of this class, hence recursive lookups from them cross this class.
44  */
45 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
46         extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
47
48     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
49
50     private final @NonNull SourceSpecificContext sourceContext;
51     private final A argument;
52
53     private YangVersion rootVersion;
54     private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
55     private SourceIdentifier rootIdentifier;
56
57     /**
58      * References to RootStatementContext of submodules which are included in this source.
59      */
60     private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
61
62     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
63         final StatementSourceReference ref, final String rawArgument) {
64         super(def, ref, rawArgument);
65         this.sourceContext = requireNonNull(sourceContext);
66         this.argument = def.parseArgumentValue(this, rawStatementArgument());
67     }
68
69     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
70             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
71             final SourceIdentifier identifier) {
72         this(sourceContext, def, ref, rawArgument);
73         this.setRootVersion(version);
74         this.setRootIdentifier(identifier);
75     }
76
77     @Override
78     public StatementContextBase<?, ?, ?> getParentContext() {
79         // null as root cannot have parent
80         return null;
81     }
82
83     @Override
84     public NamespaceStorageNode getParentNamespaceStorage() {
85         // namespace storage of source context
86         return sourceContext;
87     }
88
89     @Override
90     public StorageNodeType getStorageNodeType() {
91         return StorageNodeType.ROOT_STATEMENT_LOCAL;
92     }
93
94     @Override
95     public RootStatementContext<?, ?, ?> getRoot() {
96         // this as its own root
97         return this;
98     }
99
100     SourceSpecificContext getSourceContext() {
101         return sourceContext;
102     }
103
104     @Override
105     public A getStatementArgument() {
106         return argument;
107     }
108
109     @Override
110     @Deprecated
111     public Optional<SchemaPath> getSchemaPath() {
112         return Optional.of(SchemaPath.ROOT);
113     }
114
115     @Override
116     public boolean isConfiguration() {
117         return true;
118     }
119
120     @Override
121     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
122             final V value) {
123         if (IncludedModuleContext.class.isAssignableFrom(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, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> 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, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
143             final K key, 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, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> 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, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> 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     boolean isEnabledSemanticVersioningImpl() {
228         return sourceContext.globalContext().isEnabledSemanticVersioning();
229     }
230
231     @NonNull YangVersion getRootVersionImpl() {
232         return rootVersion == null ? DEFAULT_VERSION : rootVersion;
233     }
234
235     void setRootVersionImpl(final YangVersion version) {
236         checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
237                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
238         checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
239                 this.rootVersion);
240         this.rootVersion = requireNonNull(version);
241     }
242
243     void addMutableStmtToSealImpl(final MutableStatement mutableStatement) {
244         sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
245     }
246
247     void addRequiredSourceImpl(final SourceIdentifier dependency) {
248         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
249                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
250         if (requiredSources.isEmpty()) {
251             requiredSources = new HashSet<>();
252         }
253         requiredSources.add(dependency);
254     }
255
256     @Override
257     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
258         throw new UnsupportedOperationException("Root statement cannot be reparented to" + newParent);
259     }
260 }