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.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
47 * of this class, hence recursive lookups from them cross this class.
49 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
50 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
51 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
53 private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
54 // These namespaces are well-known and not needed after the root is cleaned up
55 private static final Map<Class<?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
56 GroupingNamespace.class, new SweptNamespace(GroupingNamespace.class),
57 SchemaTreeNamespace.class, new SweptNamespace(SchemaTreeNamespace.class),
58 TypeNamespace.class, new SweptNamespace(TypeNamespace.class));
60 private final @NonNull SourceSpecificContext sourceContext;
61 private final A argument;
63 private YangVersion rootVersion;
64 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
65 private SourceIdentifier rootIdentifier;
68 * References to RootStatementContext of submodules which are included in this source.
70 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
72 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
73 final StatementSourceReference ref, final String rawArgument) {
74 super(def, ref, rawArgument);
75 this.sourceContext = requireNonNull(sourceContext);
76 this.argument = def.parseArgumentValue(this, rawArgument());
79 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
80 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
81 final SourceIdentifier identifier) {
82 this(sourceContext, def, ref, rawArgument);
83 this.setRootVersion(version);
84 this.setRootIdentifier(identifier);
88 public StatementContextBase<?, ?, ?> getParentContext() {
89 // null as root cannot have parent
94 public NamespaceStorageNode getParentNamespaceStorage() {
95 // namespace storage of source context
100 public StorageNodeType getStorageNodeType() {
101 return StorageNodeType.ROOT_STATEMENT_LOCAL;
105 public RootStatementContext<?, ?, ?> getRoot() {
106 // this as its own root
110 SourceSpecificContext getSourceContext() {
111 return sourceContext;
115 public A argument() {
120 public EffectiveConfig effectiveConfig() {
121 return EffectiveConfig.UNDETERMINED;
125 public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
127 if (IncludedModuleContext.class.isAssignableFrom(type)) {
128 if (includedContexts.isEmpty()) {
129 includedContexts = new ArrayList<>(1);
131 verify(value instanceof RootStatementContext);
132 includedContexts.add((RootStatementContext<?, ?, ?>) value);
134 return super.putToLocalStorage(type, key, value);
138 public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
139 return getFromLocalStorage(type, key, new HashSet<>());
143 * We need to track already checked RootStatementContexts due to possible
144 * circular chains of includes between submodules
146 private <K, V, N extends ParserNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
147 final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
148 final V potentialLocal = super.getFromLocalStorage(type, key);
149 if (potentialLocal != null) {
150 return potentialLocal;
153 alreadyChecked.add(this);
154 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
155 if (alreadyChecked.contains(includedSource)) {
158 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
159 if (potential != null) {
167 public <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
168 return getAllFromLocalStorage(type, new HashSet<>());
172 * We need to track already checked RootStatementContexts due to possible
173 * circular chains of includes between submodules
175 private <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
176 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
177 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
178 if (potentialLocal != null) {
179 return potentialLocal;
182 alreadyChecked.add(this);
183 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
184 if (alreadyChecked.contains(includedSource)) {
187 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
188 if (potential != null) {
196 * Return the set of required sources.
198 * @return Required sources.
200 Collection<SourceIdentifier> getRequiredSources() {
201 return ImmutableSet.copyOf(requiredSources);
204 SourceIdentifier getRootIdentifier() {
205 return rootIdentifier;
209 protected boolean isIgnoringIfFeatures() {
214 protected boolean isIgnoringConfig() {
219 protected boolean isParentSupportedByFeatures() {
223 void setRootIdentifierImpl(final SourceIdentifier identifier) {
224 this.rootIdentifier = requireNonNull(identifier);
227 @NonNull Registry getBehaviourRegistryImpl() {
228 return sourceContext;
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, sourceReference());
238 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
240 this.rootVersion = requireNonNull(version);
244 * Add mutable statement to seal. Each mutable statement must be sealed
245 * as the last step of statement parser processing.
247 * @param mutableStatement
248 * mutable statement which should be sealed
250 void addMutableStmtToSeal(final MutableStatement mutableStatement) {
251 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
254 void addRequiredSourceImpl(final SourceIdentifier dependency) {
255 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
256 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
257 if (requiredSources.isEmpty()) {
258 requiredSources = new HashSet<>();
260 requiredSources.add(dependency);
264 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
265 throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
269 void sweepNamespaces() {
270 LOG.trace("Sweeping root {}", this);
271 sweepNamespaces(SWEPT_NAMESPACES);