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.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;
18 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.YangVersion;
22 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
32 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
36 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
37 * of this class, hence recursive lookups from them cross this class.
39 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
40 StatementContextBase<A, D, E> {
42 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
44 private final SourceSpecificContext sourceContext;
45 private final A argument;
47 private YangVersion version;
48 private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
49 private ModuleIdentifier identifier;
52 * References to RootStatementContext of submodules which are included in this source.
54 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
56 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
57 final StatementSourceReference ref, final String rawArgument) {
58 super(def, ref, rawArgument);
59 this.sourceContext = Preconditions.checkNotNull(sourceContext);
60 this.argument = def.parseArgumentValue(this, rawStatementArgument());
63 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
64 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
65 final ModuleIdentifier identifier) {
66 this(sourceContext, def, ref, rawArgument);
67 this.setRootVersion(version);
68 this.setRootIdentifier(identifier);
72 public StatementContextBase<?, ?, ?> getParentContext() {
73 // null as root cannot have parent
78 public NamespaceStorageNode getParentNamespaceStorage() {
79 // namespace storage of source context
84 public Registry getBehaviourRegistry() {
89 public StorageNodeType getStorageNodeType() {
90 return StorageNodeType.ROOT_STATEMENT_LOCAL;
95 public RootStatementContext<?, ?, ?> getRoot() {
96 // this as its own root
100 SourceSpecificContext getSourceContext() {
101 return sourceContext;
105 public A getStatementArgument() {
111 public Optional<SchemaPath> getSchemaPath() {
112 return Optional.of(SchemaPath.ROOT);
116 public boolean isConfiguration() {
121 public boolean isEnabledSemanticVersioning() {
122 return sourceContext.isEnabledSemanticVersioning();
126 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
128 if (IncludedModuleContext.class.isAssignableFrom(type)) {
129 if (includedContexts.isEmpty()) {
130 includedContexts = new ArrayList<>(1);
132 Verify.verify(value instanceof RootStatementContext);
133 includedContexts.add((RootStatementContext<?, ?, ?>) value);
135 return super.putToLocalStorage(type, key, value);
140 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
141 return getFromLocalStorage(type, key, new HashSet<>());
145 * We need to track already checked RootStatementContexts due to possible
146 * circular chains of includes between submodules
149 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
150 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
151 final V potentialLocal = super.getFromLocalStorage(type, key);
152 if (potentialLocal != null) {
153 return potentialLocal;
156 alreadyChecked.add(this);
157 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
158 if (alreadyChecked.contains(includedSource)) {
161 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
162 if (potential != null) {
171 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
172 return getAllFromLocalStorage(type, new HashSet<>());
176 * We need to track already checked RootStatementContexts due to possible
177 * circular chains of includes between submodules
180 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
181 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
182 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
183 if (potentialLocal != null) {
184 return potentialLocal;
187 alreadyChecked.add(this);
188 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
189 if (alreadyChecked.contains(includedSource)) {
192 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
193 if (potential != null) {
201 public YangVersion getRootVersion() {
202 return version == null ? DEFAULT_VERSION : version;
206 public void setRootVersion(final YangVersion version) {
207 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
208 "Unsupported yang version %s in %s", version, getStatementSourceReference());
209 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
211 this.version = Preconditions.checkNotNull(version);
215 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
216 sourceContext.addMutableStmtToSeal(mutableStatement);
220 public void addRequiredModule(final ModuleIdentifier dependency) {
221 Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
222 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
223 if (requiredModules.isEmpty()) {
224 requiredModules = new HashSet<>();
226 requiredModules.add(dependency);
229 Collection<ModuleIdentifier> getRequiredModules() {
230 return ImmutableSet.copyOf(requiredModules);
234 public void setRootIdentifier(final ModuleIdentifier identifier) {
235 Preconditions.checkNotNull(identifier);
236 this.identifier = identifier;
239 ModuleIdentifier getRootIdentifier() {
244 protected boolean isIgnoringIfFeatures() {
249 protected boolean isIgnoringConfig() {
254 protected boolean isParentSupportedByFeatures() {