2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
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;
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;
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.
42 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
43 StatementContextBase<A, D, E> {
45 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
47 private final SourceSpecificContext sourceContext;
48 private final A argument;
50 private YangVersion version;
51 private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
52 private ModuleIdentifier identifier;
55 * References to RootStatementContext of submodules which are included in this source.
57 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
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());
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);
74 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
75 final CopyType typeOfCopy) {
78 sourceContext = Preconditions.checkNotNull(original.sourceContext);
79 this.argument = original.argument;
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());
85 for (final Mutable<?, ?, ?> stmtContext : declared) {
86 if (stmtContext.isSupportedByFeatures()) {
87 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
90 for (final StmtContext<?, ?, ?> stmtContext : effective) {
91 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
94 addEffectiveSubstatements(buffer);
98 * @return null as root cannot have parent
101 public StatementContextBase<?, ?, ?> getParentContext() {
106 * @return namespace storage of source context
109 public NamespaceStorageNode getParentNamespaceStorage() {
110 return sourceContext;
114 public Registry getBehaviourRegistry() {
115 return sourceContext;
119 public StorageNodeType getStorageNodeType() {
120 return StorageNodeType.ROOT_STATEMENT_LOCAL;
123 * @return this as its own root
127 public RootStatementContext<?, ?, ?> getRoot() {
131 SourceSpecificContext getSourceContext() {
132 return sourceContext;
136 public A getStatementArgument() {
141 * @return copy of this considering {@link CopyType} (augment, uses)
143 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
146 public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
147 final CopyType typeOfCopy) {
148 return createCopy(null, newParent, typeOfCopy);
152 * @return copy of this considering {@link CopyType} (augment, uses)
154 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
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);
161 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
163 if (this.getOriginalCtx() != null) {
164 copy.setOriginalCtx(this.getOriginalCtx());
166 copy.setOriginalCtx(this);
168 definition().onStatementAdded(copy);
174 public Optional<SchemaPath> getSchemaPath() {
175 return Optional.of(SchemaPath.ROOT);
182 public boolean isRootContext() {
187 public boolean isConfiguration() {
192 public boolean isInYangDataExtensionBody() {
197 public boolean isEnabledSemanticVersioning() {
198 return sourceContext.isEnabledSemanticVersioning();
202 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
204 if (IncludedModuleContext.class.isAssignableFrom(type)) {
205 if (includedContexts.isEmpty()) {
206 includedContexts = new ArrayList<>(1);
208 Verify.verify(value instanceof RootStatementContext);
209 includedContexts.add((RootStatementContext<?, ?, ?>) value);
211 return super.putToLocalStorage(type, key, value);
216 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
217 return getFromLocalStorage(type, key, new HashSet<>());
221 * We need to track already checked RootStatementContexts due to possible
222 * circular chains of includes between submodules
225 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
226 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
227 final V potentialLocal = super.getFromLocalStorage(type, key);
228 if (potentialLocal != null) {
229 return potentialLocal;
232 alreadyChecked.add(this);
233 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
234 if (alreadyChecked.contains(includedSource)) {
237 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
238 if (potential != null) {
247 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
248 return getAllFromLocalStorage(type, new HashSet<>());
252 * We need to track already checked RootStatementContexts due to possible
253 * circular chains of includes between submodules
256 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
257 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
258 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
259 if (potentialLocal != null) {
260 return potentialLocal;
263 alreadyChecked.add(this);
264 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
265 if (alreadyChecked.contains(includedSource)) {
268 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
269 if (potential != null) {
277 public YangVersion getRootVersion() {
278 return version == null ? DEFAULT_VERSION : version;
282 public void setRootVersion(final YangVersion version) {
283 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
284 "Unsupported yang version %s in %s", version, getStatementSourceReference());
285 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
287 this.version = Preconditions.checkNotNull(version);
291 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
292 sourceContext.addMutableStmtToSeal(mutableStatement);
296 public void addRequiredModule(final ModuleIdentifier dependency) {
297 Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
298 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
299 if (requiredModules.isEmpty()) {
300 requiredModules = new HashSet<>();
302 requiredModules.add(dependency);
305 Collection<ModuleIdentifier> getRequiredModules() {
306 return ImmutableSet.copyOf(requiredModules);
310 public void setRootIdentifier(final ModuleIdentifier identifier) {
311 Preconditions.checkNotNull(identifier);
312 this.identifier = identifier;
315 ModuleIdentifier getRootIdentifier() {