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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static java.util.Objects.requireNonNull;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashSet;
21 import java.util.Optional;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
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<SourceIdentifier> requiredSources = ImmutableSet.of();
52 private SourceIdentifier 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 = requireNonNull(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 SourceIdentifier identifier) {
69 this(sourceContext, def, ref, rawArgument);
70 this.setRootVersion(version);
71 this.setRootIdentifier(identifier);
75 public StatementContextBase<?, ?, ?> getParentContext() {
76 // null as root cannot have parent
81 public NamespaceStorageNode getParentNamespaceStorage() {
82 // namespace storage of source context
87 public Registry getBehaviourRegistry() {
92 public StorageNodeType getStorageNodeType() {
93 return StorageNodeType.ROOT_STATEMENT_LOCAL;
98 public RootStatementContext<?, ?, ?> getRoot() {
99 // this as its own root
103 SourceSpecificContext getSourceContext() {
104 return sourceContext;
108 public A getStatementArgument() {
114 public Optional<SchemaPath> getSchemaPath() {
115 return Optional.of(SchemaPath.ROOT);
119 public boolean isConfiguration() {
124 public boolean isEnabledSemanticVersioning() {
125 return sourceContext.isEnabledSemanticVersioning();
129 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
131 if (IncludedModuleContext.class.isAssignableFrom(type)) {
132 if (includedContexts.isEmpty()) {
133 includedContexts = new ArrayList<>(1);
135 verify(value instanceof RootStatementContext);
136 includedContexts.add((RootStatementContext<?, ?, ?>) value);
138 return super.putToLocalStorage(type, key, value);
143 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
144 return getFromLocalStorage(type, key, new HashSet<>());
148 * We need to track already checked RootStatementContexts due to possible
149 * circular chains of includes between submodules
152 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
153 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
154 final V potentialLocal = super.getFromLocalStorage(type, key);
155 if (potentialLocal != null) {
156 return potentialLocal;
159 alreadyChecked.add(this);
160 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
161 if (alreadyChecked.contains(includedSource)) {
164 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
165 if (potential != null) {
174 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
175 return getAllFromLocalStorage(type, new HashSet<>());
179 * We need to track already checked RootStatementContexts due to possible
180 * circular chains of includes between submodules
183 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
184 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
185 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
186 if (potentialLocal != null) {
187 return potentialLocal;
190 alreadyChecked.add(this);
191 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
192 if (alreadyChecked.contains(includedSource)) {
195 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
196 if (potential != null) {
204 public YangVersion getRootVersion() {
205 return version == null ? DEFAULT_VERSION : version;
209 public void setRootVersion(final YangVersion version) {
210 checkArgument(sourceContext.getSupportedVersions().contains(version),
211 "Unsupported yang version %s in %s", version, getStatementSourceReference());
212 checkState(this.version == null, "Version of root %s has been already set to %s", argument,
214 this.version = requireNonNull(version);
218 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
219 sourceContext.addMutableStmtToSeal(mutableStatement);
223 public void addRequiredSource(final SourceIdentifier dependency) {
224 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
225 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
226 if (requiredSources.isEmpty()) {
227 requiredSources = new HashSet<>();
229 requiredSources.add(dependency);
233 * Return the set of required sources.
235 * @return Required sources.
237 Collection<SourceIdentifier> getRequiredSources() {
238 return ImmutableSet.copyOf(requiredSources);
242 public void setRootIdentifier(final SourceIdentifier identifier) {
243 this.identifier = requireNonNull(identifier);
246 SourceIdentifier getRootIdentifier() {
251 protected boolean isIgnoringIfFeatures() {
256 protected boolean isIgnoringConfig() {
261 protected boolean isParentSupportedByFeatures() {