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