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.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Optional;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.yangtools.yang.common.YangVersion;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
43 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
49 * of this class, hence recursive lookups from them cross this class.
51 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
52 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
53 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
55 private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
56 // These namespaces are well-known and not needed after the root is cleaned up
57 private static final Map<Class<?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
58 GroupingNamespace.class, new SweptNamespace(GroupingNamespace.class),
59 SchemaTreeNamespace.class, new SweptNamespace(SchemaTreeNamespace.class),
60 TypeNamespace.class, new SweptNamespace(TypeNamespace.class));
62 private final @NonNull SourceSpecificContext sourceContext;
63 private final A argument;
65 private YangVersion rootVersion;
66 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
67 private SourceIdentifier rootIdentifier;
70 * References to RootStatementContext of submodules which are included in this source.
72 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
74 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
75 final StatementSourceReference ref, final String rawArgument) {
76 super(def, ref, rawArgument);
77 this.sourceContext = requireNonNull(sourceContext);
78 this.argument = def.parseArgumentValue(this, rawArgument());
81 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
82 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
83 final SourceIdentifier identifier) {
84 this(sourceContext, def, ref, rawArgument);
85 this.setRootVersion(version);
86 this.setRootIdentifier(identifier);
90 public StatementContextBase<?, ?, ?> getParentContext() {
91 // null as root cannot have parent
96 public NamespaceStorageNode getParentNamespaceStorage() {
97 // namespace storage of source context
102 public StorageNodeType getStorageNodeType() {
103 return StorageNodeType.ROOT_STATEMENT_LOCAL;
107 public RootStatementContext<?, ?, ?> getRoot() {
108 // this as its own root
112 SourceSpecificContext getSourceContext() {
113 return sourceContext;
117 public A argument() {
123 public Optional<SchemaPath> schemaPath() {
124 return Optional.of(SchemaPath.ROOT);
128 public EffectiveConfig effectiveConfig() {
129 return EffectiveConfig.UNDETERMINED;
133 public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
135 if (IncludedModuleContext.class.isAssignableFrom(type)) {
136 if (includedContexts.isEmpty()) {
137 includedContexts = new ArrayList<>(1);
139 verify(value instanceof RootStatementContext);
140 includedContexts.add((RootStatementContext<?, ?, ?>) value);
142 return super.putToLocalStorage(type, key, value);
146 public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
147 return getFromLocalStorage(type, key, new HashSet<>());
151 * We need to track already checked RootStatementContexts due to possible
152 * circular chains of includes between submodules
154 private <K, V, N extends ParserNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
155 final K key, 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) {
175 public <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
176 return getAllFromLocalStorage(type, new HashSet<>());
180 * We need to track already checked RootStatementContexts due to possible
181 * circular chains of includes between submodules
183 private <K, V, N extends ParserNamespace<K, V>> @Nullable 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 * Return the set of required sources.
206 * @return Required sources.
208 Collection<SourceIdentifier> getRequiredSources() {
209 return ImmutableSet.copyOf(requiredSources);
212 SourceIdentifier getRootIdentifier() {
213 return rootIdentifier;
217 protected boolean isIgnoringIfFeatures() {
222 protected boolean isIgnoringConfig() {
227 protected boolean isParentSupportedByFeatures() {
231 void setRootIdentifierImpl(final SourceIdentifier identifier) {
232 this.rootIdentifier = requireNonNull(identifier);
235 @NonNull Registry getBehaviourRegistryImpl() {
236 return sourceContext;
239 boolean isEnabledSemanticVersioningImpl() {
240 return sourceContext.globalContext().isEnabledSemanticVersioning();
243 @NonNull YangVersion getRootVersionImpl() {
244 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
247 void setRootVersionImpl(final YangVersion version) {
248 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
249 "Unsupported yang version %s in %s", version, sourceReference());
250 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
252 this.rootVersion = requireNonNull(version);
256 * Add mutable statement to seal. Each mutable statement must be sealed
257 * as the last step of statement parser processing.
259 * @param mutableStatement
260 * mutable statement which should be sealed
262 void addMutableStmtToSeal(final MutableStatement mutableStatement) {
263 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
266 void addRequiredSourceImpl(final SourceIdentifier dependency) {
267 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
268 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
269 if (requiredSources.isEmpty()) {
270 requiredSources = new HashSet<>();
272 requiredSources.add(dependency);
276 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
277 throw new UnsupportedOperationException("Root statement cannot be reparented to" + newParent);
281 void sweepNamespaces() {
282 LOG.trace("Sweeping root {}", this);
283 sweepNamespaces(SWEPT_NAMESPACES);