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;
20 import java.util.List;
22 import java.util.Optional;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.common.YangVersion;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
30 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
40 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
41 * of this class, hence recursive lookups from them cross this class.
43 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
44 StatementContextBase<A, D, E> {
46 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
48 private final SourceSpecificContext sourceContext;
49 private final A argument;
51 private YangVersion rootVersion;
52 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
53 private SourceIdentifier rootIdentifier;
56 * References to RootStatementContext of submodules which are included in this source.
58 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
60 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
61 final StatementSourceReference ref, final String rawArgument) {
62 super(def, ref, rawArgument);
63 this.sourceContext = requireNonNull(sourceContext);
64 this.argument = def.parseArgumentValue(this, rawStatementArgument());
67 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
68 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
69 final SourceIdentifier identifier) {
70 this(sourceContext, def, ref, rawArgument);
71 this.setRootVersion(version);
72 this.setRootIdentifier(identifier);
76 public StatementContextBase<?, ?, ?> getParentContext() {
77 // null as root cannot have parent
82 public NamespaceStorageNode getParentNamespaceStorage() {
83 // namespace storage of source context
88 public Registry getBehaviourRegistry() {
93 public StorageNodeType getStorageNodeType() {
94 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() {
113 public Optional<SchemaPath> getSchemaPath() {
114 return Optional.of(SchemaPath.ROOT);
118 public boolean isConfiguration() {
123 public boolean isEnabledSemanticVersioning() {
124 return sourceContext.isEnabledSemanticVersioning();
128 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
130 if (IncludedModuleContext.class.isAssignableFrom(type)) {
131 if (includedContexts.isEmpty()) {
132 includedContexts = new ArrayList<>(1);
134 verify(value instanceof RootStatementContext);
135 includedContexts.add((RootStatementContext<?, ?, ?>) value);
137 return super.putToLocalStorage(type, key, value);
141 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
142 return getFromLocalStorage(type, key, new HashSet<>());
146 * We need to track already checked RootStatementContexts due to possible
147 * circular chains of includes between submodules
149 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
150 final K key, 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) {
170 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
171 return getAllFromLocalStorage(type, new HashSet<>());
175 * We need to track already checked RootStatementContexts due to possible
176 * circular chains of includes between submodules
178 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
179 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
180 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
181 if (potentialLocal != null) {
182 return potentialLocal;
185 alreadyChecked.add(this);
186 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
187 if (alreadyChecked.contains(includedSource)) {
190 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
191 if (potential != null) {
199 public YangVersion getRootVersion() {
200 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
204 public void setRootVersion(final YangVersion version) {
205 checkArgument(sourceContext.getSupportedVersions().contains(version),
206 "Unsupported yang version %s in %s", version, getStatementSourceReference());
207 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
209 this.rootVersion = requireNonNull(version);
213 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
214 sourceContext.addMutableStmtToSeal(mutableStatement);
218 public void addRequiredSource(final SourceIdentifier dependency) {
219 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
220 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
221 if (requiredSources.isEmpty()) {
222 requiredSources = new HashSet<>();
224 requiredSources.add(dependency);
228 * Return the set of required sources.
230 * @return Required sources.
232 Collection<SourceIdentifier> getRequiredSources() {
233 return ImmutableSet.copyOf(requiredSources);
237 public void setRootIdentifier(final SourceIdentifier identifier) {
238 this.rootIdentifier = requireNonNull(identifier);
241 SourceIdentifier getRootIdentifier() {
242 return rootIdentifier;
246 protected boolean isIgnoringIfFeatures() {
251 protected boolean isIgnoringConfig() {
256 protected boolean isParentSupportedByFeatures() {