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 * @return null as root cannot have parent
75 public StatementContextBase<?, ?, ?> getParentContext() {
80 * @return namespace storage of source context
83 public NamespaceStorageNode getParentNamespaceStorage() {
88 public Registry getBehaviourRegistry() {
93 public StorageNodeType getStorageNodeType() {
94 return StorageNodeType.ROOT_STATEMENT_LOCAL;
97 * @return this as its own root
101 public RootStatementContext<?, ?, ?> getRoot() {
105 SourceSpecificContext getSourceContext() {
106 return sourceContext;
110 public A getStatementArgument() {
116 public Optional<SchemaPath> getSchemaPath() {
117 return Optional.of(SchemaPath.ROOT);
121 public boolean isConfiguration() {
126 public boolean isEnabledSemanticVersioning() {
127 return sourceContext.isEnabledSemanticVersioning();
131 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
133 if (IncludedModuleContext.class.isAssignableFrom(type)) {
134 if (includedContexts.isEmpty()) {
135 includedContexts = new ArrayList<>(1);
137 Verify.verify(value instanceof RootStatementContext);
138 includedContexts.add((RootStatementContext<?, ?, ?>) value);
140 return super.putToLocalStorage(type, key, value);
145 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
146 return getFromLocalStorage(type, key, new HashSet<>());
150 * We need to track already checked RootStatementContexts due to possible
151 * circular chains of includes between submodules
154 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
155 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
156 final V potentialLocal = super.getFromLocalStorage(type, key);
157 if (potentialLocal != null) {
158 return potentialLocal;
161 alreadyChecked.add(this);
162 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
163 if (alreadyChecked.contains(includedSource)) {
166 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
167 if (potential != null) {
176 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
177 return getAllFromLocalStorage(type, new HashSet<>());
181 * We need to track already checked RootStatementContexts due to possible
182 * circular chains of includes between submodules
185 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
186 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
187 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
188 if (potentialLocal != null) {
189 return potentialLocal;
192 alreadyChecked.add(this);
193 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
194 if (alreadyChecked.contains(includedSource)) {
197 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
198 if (potential != null) {
206 public YangVersion getRootVersion() {
207 return version == null ? DEFAULT_VERSION : version;
211 public void setRootVersion(final YangVersion version) {
212 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
213 "Unsupported yang version %s in %s", version, getStatementSourceReference());
214 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
216 this.version = Preconditions.checkNotNull(version);
220 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
221 sourceContext.addMutableStmtToSeal(mutableStatement);
225 public void addRequiredModule(final ModuleIdentifier dependency) {
226 Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
227 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
228 if (requiredModules.isEmpty()) {
229 requiredModules = new HashSet<>();
231 requiredModules.add(dependency);
234 Collection<ModuleIdentifier> getRequiredModules() {
235 return ImmutableSet.copyOf(requiredModules);
239 public void setRootIdentifier(final ModuleIdentifier identifier) {
240 Preconditions.checkNotNull(identifier);
241 this.identifier = identifier;
244 ModuleIdentifier getRootIdentifier() {
249 protected boolean isIgnoringIfFeatures() {
254 protected boolean isIgnoringConfig() {
259 protected boolean isParentSupportedByFeatures() {