Bug 8922 - Evaluation of if-features is done regardless of ancestors
[yangtools.git] / yang / yang-parser-impl / 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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
34 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
36
37 /**
38  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
39  * of this class, hence recursive lookups from them cross this class.
40  */
41 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
42         StatementContextBase<A, D, E> {
43
44     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
45
46     private final SourceSpecificContext sourceContext;
47     private final A argument;
48
49     private YangVersion version;
50     private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
51     private ModuleIdentifier identifier;
52
53     /**
54      * References to RootStatementContext of submodules which are included in this source.
55      */
56     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
57
58     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
59         final StatementSourceReference ref, final String rawArgument) {
60         super(def, ref, rawArgument);
61         this.sourceContext = Preconditions.checkNotNull(sourceContext);
62         this.argument = def.parseArgumentValue(this, rawStatementArgument());
63     }
64
65     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
66             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
67             final ModuleIdentifier identifier) {
68         this(sourceContext, def, ref, rawArgument);
69         this.setRootVersion(version);
70         this.setRootIdentifier(identifier);
71     }
72
73     /**
74      * @return null as root cannot have parent
75      */
76     @Override
77     public StatementContextBase<?, ?, ?> getParentContext() {
78         return null;
79     }
80
81     /**
82      * @return namespace storage of source context
83      */
84     @Override
85     public NamespaceStorageNode getParentNamespaceStorage() {
86         return sourceContext;
87     }
88
89     @Override
90     public Registry getBehaviourRegistry() {
91         return sourceContext;
92     }
93
94     @Override
95     public StorageNodeType getStorageNodeType() {
96         return StorageNodeType.ROOT_STATEMENT_LOCAL;
97     }
98     /**
99      * @return this as its own root
100      */
101     @Nonnull
102     @Override
103     public RootStatementContext<?, ?, ?> getRoot() {
104         return this;
105     }
106
107     SourceSpecificContext getSourceContext() {
108         return sourceContext;
109     }
110
111     @Override
112     public A getStatementArgument() {
113         return argument;
114     }
115
116     @Override
117     public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
118             final CopyType typeOfCopy) {
119         throw new UnsupportedOperationException("Root context cannot be copied");
120     }
121
122     @Override
123     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
124             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
125         throw new UnsupportedOperationException("Root context cannot be copied");
126     }
127
128     @Nonnull
129     @Override
130     public Optional<SchemaPath> getSchemaPath() {
131         return Optional.of(SchemaPath.ROOT);
132     }
133
134     @Override
135     public boolean isConfiguration() {
136         return true;
137     }
138
139     @Override
140     public boolean isEnabledSemanticVersioning() {
141         return sourceContext.isEnabledSemanticVersioning();
142     }
143
144     @Override
145     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
146             final V value) {
147         if (IncludedModuleContext.class.isAssignableFrom(type)) {
148             if (includedContexts.isEmpty()) {
149                 includedContexts = new ArrayList<>(1);
150             }
151             Verify.verify(value instanceof RootStatementContext);
152             includedContexts.add((RootStatementContext<?, ?, ?>) value);
153         }
154         return super.putToLocalStorage(type, key, value);
155     }
156
157     @Nullable
158     @Override
159     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
160         return getFromLocalStorage(type, key, new HashSet<>());
161     }
162
163     /*
164      * We need to track already checked RootStatementContexts due to possible
165      * circular chains of includes between submodules
166      */
167     @Nullable
168     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
169             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
170         final V potentialLocal = super.getFromLocalStorage(type, key);
171         if (potentialLocal != null) {
172             return potentialLocal;
173         }
174
175         alreadyChecked.add(this);
176         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
177             if (alreadyChecked.contains(includedSource)) {
178                 continue;
179             }
180             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
181             if (potential != null) {
182                 return potential;
183             }
184         }
185         return null;
186     }
187
188     @Nullable
189     @Override
190     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
191         return getAllFromLocalStorage(type, new HashSet<>());
192     }
193
194     /*
195      * We need to track already checked RootStatementContexts due to possible
196      * circular chains of includes between submodules
197      */
198     @Nullable
199     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
200             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
201         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
202         if (potentialLocal != null) {
203             return potentialLocal;
204         }
205
206         alreadyChecked.add(this);
207         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
208             if (alreadyChecked.contains(includedSource)) {
209                 continue;
210             }
211             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
212             if (potential != null) {
213                 return potential;
214             }
215         }
216         return null;
217     }
218
219     @Override
220     public YangVersion getRootVersion() {
221         return version == null ? DEFAULT_VERSION : version;
222     }
223
224     @Override
225     public void setRootVersion(final YangVersion version) {
226         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
227                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
228         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
229                 this.version);
230         this.version = Preconditions.checkNotNull(version);
231     }
232
233     @Override
234     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
235         sourceContext.addMutableStmtToSeal(mutableStatement);
236     }
237
238     @Override
239     public void addRequiredModule(final ModuleIdentifier dependency) {
240         Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
241                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
242         if (requiredModules.isEmpty()) {
243             requiredModules = new HashSet<>();
244         }
245         requiredModules.add(dependency);
246     }
247
248     Collection<ModuleIdentifier> getRequiredModules() {
249         return ImmutableSet.copyOf(requiredModules);
250     }
251
252     @Override
253     public void setRootIdentifier(final ModuleIdentifier identifier) {
254         Preconditions.checkNotNull(identifier);
255         this.identifier = identifier;
256     }
257
258     ModuleIdentifier getRootIdentifier() {
259         return identifier;
260     }
261
262     @Override
263     protected boolean isIgnoringIfFeatures() {
264         return false;
265     }
266
267     @Override
268     protected boolean isIgnoringConfig() {
269         return false;
270     }
271
272     @Override
273     protected boolean isParentSupportedByFeatures() {
274         return true;
275     }
276 }