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;
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.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
48 * of this class, hence recursive lookups from them cross this class.
50 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
51 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
52 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
54 private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
55 // These namespaces are well-known and not needed after the root is cleaned up
56 private static final Map<Class<?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
57 GroupingNamespace.class, new SweptNamespace(GroupingNamespace.class),
58 SchemaTreeNamespace.class, new SweptNamespace(SchemaTreeNamespace.class),
59 TypeNamespace.class, new SweptNamespace(TypeNamespace.class));
61 private final @NonNull SourceSpecificContext sourceContext;
62 private final A argument;
64 private YangVersion rootVersion;
65 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
66 private SourceIdentifier rootIdentifier;
69 * References to RootStatementContext of submodules which are included in this source.
71 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
73 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
74 final StatementSourceReference ref, final String rawArgument) {
75 super(def, ref, rawArgument);
76 this.sourceContext = requireNonNull(sourceContext);
77 this.argument = def.parseArgumentValue(this, rawArgument());
80 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
81 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
82 final SourceIdentifier identifier) {
83 this(sourceContext, def, ref, rawArgument);
84 this.setRootVersion(version);
85 this.setRootIdentifier(identifier);
89 public StatementContextBase<?, ?, ?> getParentContext() {
90 // null as root cannot have parent
95 public NamespaceStorageNode getParentNamespaceStorage() {
96 // namespace storage of source context
101 public StorageNodeType getStorageNodeType() {
102 return StorageNodeType.ROOT_STATEMENT_LOCAL;
106 public RootStatementContext<?, ?, ?> getRoot() {
107 // this as its own root
111 SourceSpecificContext getSourceContext() {
112 return sourceContext;
116 public A argument() {
122 public SchemaPath schemaPath() {
123 return SchemaPath.ROOT;
127 public EffectiveConfig effectiveConfig() {
128 return EffectiveConfig.UNDETERMINED;
132 public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
134 if (IncludedModuleContext.class.isAssignableFrom(type)) {
135 if (includedContexts.isEmpty()) {
136 includedContexts = new ArrayList<>(1);
138 verify(value instanceof RootStatementContext);
139 includedContexts.add((RootStatementContext<?, ?, ?>) value);
141 return super.putToLocalStorage(type, key, value);
145 public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
146 return getFromLocalStorage(type, key, new HashSet<>());
150 * We need to track already checked RootStatementContexts due to possible
151 * circular chains of includes between submodules
153 private <K, V, N extends ParserNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
154 final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
155 final V potentialLocal = super.getFromLocalStorage(type, key);
156 if (potentialLocal != null) {
157 return potentialLocal;
160 alreadyChecked.add(this);
161 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
162 if (alreadyChecked.contains(includedSource)) {
165 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
166 if (potential != null) {
174 public <K, V, N extends ParserNamespace<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
182 private <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
183 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
184 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
185 if (potentialLocal != null) {
186 return potentialLocal;
189 alreadyChecked.add(this);
190 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
191 if (alreadyChecked.contains(includedSource)) {
194 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
195 if (potential != null) {
203 * Return the set of required sources.
205 * @return Required sources.
207 Collection<SourceIdentifier> getRequiredSources() {
208 return ImmutableSet.copyOf(requiredSources);
211 SourceIdentifier getRootIdentifier() {
212 return rootIdentifier;
216 protected boolean isIgnoringIfFeatures() {
221 protected boolean isIgnoringConfig() {
226 protected boolean isParentSupportedByFeatures() {
230 void setRootIdentifierImpl(final SourceIdentifier identifier) {
231 this.rootIdentifier = requireNonNull(identifier);
234 @NonNull Registry getBehaviourRegistryImpl() {
235 return sourceContext;
238 boolean isEnabledSemanticVersioningImpl() {
239 return sourceContext.globalContext().isEnabledSemanticVersioning();
242 @NonNull YangVersion getRootVersionImpl() {
243 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
246 void setRootVersionImpl(final YangVersion version) {
247 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
248 "Unsupported yang version %s in %s", version, sourceReference());
249 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
251 this.rootVersion = requireNonNull(version);
255 * Add mutable statement to seal. Each mutable statement must be sealed
256 * as the last step of statement parser processing.
258 * @param mutableStatement
259 * mutable statement which should be sealed
261 void addMutableStmtToSeal(final MutableStatement mutableStatement) {
262 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
265 void addRequiredSourceImpl(final SourceIdentifier dependency) {
266 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
267 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
268 if (requiredSources.isEmpty()) {
269 requiredSources = new HashSet<>();
271 requiredSources.add(dependency);
275 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
276 throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
280 void sweepNamespaces() {
281 LOG.trace("Sweeping root {}", this);
282 sweepNamespaces(SWEPT_NAMESPACES);