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.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.YangVersion;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
42 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
43 * of this class, hence recursive lookups from them cross this class.
45 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
46 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
48 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
50 private final @NonNull SourceSpecificContext sourceContext;
51 private final A argument;
53 private YangVersion rootVersion;
54 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
55 private SourceIdentifier rootIdentifier;
58 * References to RootStatementContext of submodules which are included in this source.
60 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
62 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
63 final StatementSourceReference ref, final String rawArgument) {
64 super(def, ref, rawArgument);
65 this.sourceContext = requireNonNull(sourceContext);
66 this.argument = def.parseArgumentValue(this, rawStatementArgument());
69 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
70 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
71 final SourceIdentifier identifier) {
72 this(sourceContext, def, ref, rawArgument);
73 this.setRootVersion(version);
74 this.setRootIdentifier(identifier);
78 public StatementContextBase<?, ?, ?> getParentContext() {
79 // null as root cannot have parent
84 public NamespaceStorageNode getParentNamespaceStorage() {
85 // namespace storage of source context
90 public StorageNodeType getStorageNodeType() {
91 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 <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
123 if (IncludedModuleContext.class.isAssignableFrom(type)) {
124 if (includedContexts.isEmpty()) {
125 includedContexts = new ArrayList<>(1);
127 verify(value instanceof RootStatementContext);
128 includedContexts.add((RootStatementContext<?, ?, ?>) value);
130 return super.putToLocalStorage(type, key, value);
134 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
135 return getFromLocalStorage(type, key, new HashSet<>());
139 * We need to track already checked RootStatementContexts due to possible
140 * circular chains of includes between submodules
142 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
143 final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
144 final V potentialLocal = super.getFromLocalStorage(type, key);
145 if (potentialLocal != null) {
146 return potentialLocal;
149 alreadyChecked.add(this);
150 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
151 if (alreadyChecked.contains(includedSource)) {
154 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
155 if (potential != null) {
163 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
164 return getAllFromLocalStorage(type, new HashSet<>());
168 * We need to track already checked RootStatementContexts due to possible
169 * circular chains of includes between submodules
171 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
172 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
173 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
174 if (potentialLocal != null) {
175 return potentialLocal;
178 alreadyChecked.add(this);
179 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
180 if (alreadyChecked.contains(includedSource)) {
183 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
184 if (potential != null) {
192 * Return the set of required sources.
194 * @return Required sources.
196 Collection<SourceIdentifier> getRequiredSources() {
197 return ImmutableSet.copyOf(requiredSources);
200 SourceIdentifier getRootIdentifier() {
201 return rootIdentifier;
205 protected boolean isIgnoringIfFeatures() {
210 protected boolean isIgnoringConfig() {
215 protected boolean isParentSupportedByFeatures() {
219 void setRootIdentifierImpl(final SourceIdentifier identifier) {
220 this.rootIdentifier = requireNonNull(identifier);
223 @NonNull Registry getBehaviourRegistryImpl() {
224 return sourceContext;
227 boolean isEnabledSemanticVersioningImpl() {
228 return sourceContext.globalContext().isEnabledSemanticVersioning();
231 @NonNull YangVersion getRootVersionImpl() {
232 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
235 void setRootVersionImpl(final YangVersion version) {
236 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
237 "Unsupported yang version %s in %s", version, getStatementSourceReference());
238 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
240 this.rootVersion = requireNonNull(version);
243 void addMutableStmtToSealImpl(final MutableStatement mutableStatement) {
244 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
247 void addRequiredSourceImpl(final SourceIdentifier dependency) {
248 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
249 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
250 if (requiredSources.isEmpty()) {
251 requiredSources = new HashSet<>();
253 requiredSources.add(dependency);
257 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
258 throw new UnsupportedOperationException("Root statement cannot be reparented to" + newParent);