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.ParserNamespaces;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
44 * of this class, hence recursive lookups from them cross this class.
46 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
47 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
48 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
50 private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
51 // These namespaces are well-known and not needed after the root is cleaned up
52 private static final Map<ParserNamespace<?, ?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
53 ParserNamespaces.GROUPING, new SweptNamespace(ParserNamespaces.GROUPING),
54 ParserNamespaces.schemaTree(), new SweptNamespace(ParserNamespaces.schemaTree()),
55 ParserNamespaces.TYPE, new SweptNamespace(ParserNamespaces.TYPE));
57 private final @NonNull SourceSpecificContext sourceContext;
58 private final A argument;
60 private YangVersion rootVersion;
61 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
62 private SourceIdentifier rootIdentifier;
65 * References to RootStatementContext of submodules which are included in this source.
67 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
69 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
70 final StatementSourceReference ref, final String rawArgument) {
71 super(def, ref, rawArgument);
72 this.sourceContext = requireNonNull(sourceContext);
73 this.argument = def.parseArgumentValue(this, rawArgument());
76 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
77 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
78 final SourceIdentifier identifier) {
79 this(sourceContext, def, ref, rawArgument);
80 setRootVersion(version);
81 setRootIdentifier(identifier);
85 public StatementContextBase<?, ?, ?> getParentContext() {
86 // null as root cannot have parent
91 public NamespaceStorageNode getParentNamespaceStorage() {
92 // namespace storage of source context
97 public StorageNodeType getStorageNodeType() {
98 return StorageNodeType.ROOT_STATEMENT_LOCAL;
102 public RootStatementContext<?, ?, ?> getRoot() {
103 // this as its own root
107 SourceSpecificContext getSourceContext() {
108 return sourceContext;
112 public A argument() {
117 public EffectiveConfig effectiveConfig() {
118 return EffectiveConfig.UNDETERMINED;
122 public <K, V> V putToLocalStorage(final ParserNamespace<K, V> type, final K key, final V value) {
123 if (ParserNamespaces.INCLUDED_MODULE.equals(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> V getFromLocalStorage(final ParserNamespace<K, V> 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> @Nullable V getFromLocalStorage(final ParserNamespace<K, V> type, final K key,
143 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> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> 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> @Nullable Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> 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 @NonNull YangVersion getRootVersionImpl() {
228 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
231 void setRootVersionImpl(final YangVersion version) {
232 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
233 "Unsupported yang version %s in %s", version, sourceReference());
234 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
236 this.rootVersion = requireNonNull(version);
240 * Add mutable statement to seal. Each mutable statement must be sealed
241 * as the last step of statement parser processing.
243 * @param mutableStatement
244 * mutable statement which should be sealed
246 void addMutableStmtToSeal(final MutableStatement mutableStatement) {
247 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
250 void addRequiredSourceImpl(final SourceIdentifier dependency) {
251 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
252 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
253 if (requiredSources.isEmpty()) {
254 requiredSources = new HashSet<>();
256 requiredSources.add(dependency);
261 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
262 throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
266 void sweepNamespaces() {
267 LOG.trace("Sweeping root {}", this);
268 sweepNamespaces(SWEPT_NAMESPACES);