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.source.IncludedModuleContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
41 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
42 * of this class, hence recursive lookups from them cross this class.
44 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
45 AbstractResumedStatement<A, D, E> {
47 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
49 private final @NonNull SourceSpecificContext sourceContext;
50 private final A argument;
52 private YangVersion rootVersion;
53 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
54 private SourceIdentifier rootIdentifier;
57 * References to RootStatementContext of submodules which are included in this source.
59 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
61 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
62 final StatementSourceReference ref, final String rawArgument) {
63 super(def, ref, rawArgument);
64 this.sourceContext = requireNonNull(sourceContext);
65 this.argument = def.parseArgumentValue(this, rawStatementArgument());
68 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
69 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
70 final SourceIdentifier identifier) {
71 this(sourceContext, def, ref, rawArgument);
72 this.setRootVersion(version);
73 this.setRootIdentifier(identifier);
77 public StatementContextBase<?, ?, ?> getParentContext() {
78 // null as root cannot have parent
83 public NamespaceStorageNode getParentNamespaceStorage() {
84 // namespace storage of source context
89 public StorageNodeType getStorageNodeType() {
90 return StorageNodeType.ROOT_STATEMENT_LOCAL;
94 public RootStatementContext<?, ?, ?> getRoot() {
95 // this as its own root
99 SourceSpecificContext getSourceContext() {
100 return sourceContext;
104 public A getStatementArgument() {
110 public Optional<SchemaPath> getSchemaPath() {
111 return Optional.of(SchemaPath.ROOT);
115 public boolean isConfiguration() {
120 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
122 if (IncludedModuleContext.class.isAssignableFrom(type)) {
123 if (includedContexts.isEmpty()) {
124 includedContexts = new ArrayList<>(1);
126 verify(value instanceof RootStatementContext);
127 includedContexts.add((RootStatementContext<?, ?, ?>) value);
129 return super.putToLocalStorage(type, key, value);
133 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
134 return getFromLocalStorage(type, key, new HashSet<>());
138 * We need to track already checked RootStatementContexts due to possible
139 * circular chains of includes between submodules
141 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
142 final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
143 final V potentialLocal = super.getFromLocalStorage(type, key);
144 if (potentialLocal != null) {
145 return potentialLocal;
148 alreadyChecked.add(this);
149 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
150 if (alreadyChecked.contains(includedSource)) {
153 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
154 if (potential != null) {
162 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
163 return getAllFromLocalStorage(type, new HashSet<>());
167 * We need to track already checked RootStatementContexts due to possible
168 * circular chains of includes between submodules
170 private <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
171 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
172 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
173 if (potentialLocal != null) {
174 return potentialLocal;
177 alreadyChecked.add(this);
178 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
179 if (alreadyChecked.contains(includedSource)) {
182 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
183 if (potential != null) {
191 * Return the set of required sources.
193 * @return Required sources.
195 Collection<SourceIdentifier> getRequiredSources() {
196 return ImmutableSet.copyOf(requiredSources);
199 SourceIdentifier getRootIdentifier() {
200 return rootIdentifier;
204 protected boolean isIgnoringIfFeatures() {
209 protected boolean isIgnoringConfig() {
214 protected boolean isParentSupportedByFeatures() {
218 void setRootIdentifierImpl(final SourceIdentifier identifier) {
219 this.rootIdentifier = requireNonNull(identifier);
222 @NonNull Registry getBehaviourRegistryImpl() {
223 return sourceContext;
226 boolean isEnabledSemanticVersioningImpl() {
227 return sourceContext.globalContext().isEnabledSemanticVersioning();
230 @NonNull YangVersion getRootVersionImpl() {
231 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
234 void setRootVersionImpl(final YangVersion version) {
235 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
236 "Unsupported yang version %s in %s", version, getStatementSourceReference());
237 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
239 this.rootVersion = requireNonNull(version);
242 void addMutableStmtToSealImpl(final MutableStatement mutableStatement) {
243 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
246 void addRequiredSourceImpl(final SourceIdentifier dependency) {
247 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
248 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
249 if (requiredSources.isEmpty()) {
250 requiredSources = new HashSet<>();
252 requiredSources.add(dependency);
256 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
257 throw new UnsupportedOperationException("Root statement cannot be reparented to" + newParent);