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.SchemaTreeNamespace;
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.meta.ParserNamespace;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceParserNamespaces;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
46 * of this class, hence recursive lookups from them cross this class.
48 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
49 extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
50 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
52 private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
53 // These namespaces are well-known and not needed after the root is cleaned up
54 private static final Map<ParserNamespace<?, ?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
55 ParserNamespaces.GROUPING, new SweptNamespace(ParserNamespaces.GROUPING),
56 SchemaTreeNamespace.instance(), new SweptNamespace(SchemaTreeNamespace.instance()),
57 ParserNamespaces.TYPE, new SweptNamespace(ParserNamespaces.TYPE));
59 private final @NonNull SourceSpecificContext sourceContext;
60 private final A argument;
62 private YangVersion rootVersion;
63 private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
64 private SourceIdentifier rootIdentifier;
67 * References to RootStatementContext of submodules which are included in this source.
69 private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
71 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
72 final StatementSourceReference ref, final String rawArgument) {
73 super(def, ref, rawArgument);
74 this.sourceContext = requireNonNull(sourceContext);
75 this.argument = def.parseArgumentValue(this, rawArgument());
78 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
79 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
80 final SourceIdentifier identifier) {
81 this(sourceContext, def, ref, rawArgument);
82 setRootVersion(version);
83 setRootIdentifier(identifier);
87 public StatementContextBase<?, ?, ?> getParentContext() {
88 // null as root cannot have parent
93 public NamespaceStorageNode getParentNamespaceStorage() {
94 // namespace storage of source context
99 public StorageNodeType getStorageNodeType() {
100 return StorageNodeType.ROOT_STATEMENT_LOCAL;
104 public RootStatementContext<?, ?, ?> getRoot() {
105 // this as its own root
109 SourceSpecificContext getSourceContext() {
110 return sourceContext;
114 public A argument() {
119 public EffectiveConfig effectiveConfig() {
120 return EffectiveConfig.UNDETERMINED;
124 public <K, V> V putToLocalStorage(final ParserNamespace<K, V> type, final K key, final V value) {
125 if (SourceParserNamespaces.INCLUDED_MODULE.equals(type)) {
126 if (includedContexts.isEmpty()) {
127 includedContexts = new ArrayList<>(1);
129 verify(value instanceof RootStatementContext);
130 includedContexts.add((RootStatementContext<?, ?, ?>) value);
132 return super.putToLocalStorage(type, key, value);
136 public <K, V> V getFromLocalStorage(final ParserNamespace<K, V> type, final K key) {
137 return getFromLocalStorage(type, key, new HashSet<>());
141 * We need to track already checked RootStatementContexts due to possible
142 * circular chains of includes between submodules
144 private <K, V> @Nullable V getFromLocalStorage(final ParserNamespace<K, V> type, final K key,
145 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
146 final V potentialLocal = super.getFromLocalStorage(type, key);
147 if (potentialLocal != null) {
148 return potentialLocal;
151 alreadyChecked.add(this);
152 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
153 if (alreadyChecked.contains(includedSource)) {
156 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
157 if (potential != null) {
165 public <K, V> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type) {
166 return getAllFromLocalStorage(type, new HashSet<>());
170 * We need to track already checked RootStatementContexts due to possible
171 * circular chains of includes between submodules
173 private <K, V> @Nullable Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type,
174 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
175 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
176 if (potentialLocal != null) {
177 return potentialLocal;
180 alreadyChecked.add(this);
181 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
182 if (alreadyChecked.contains(includedSource)) {
185 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
186 if (potential != null) {
194 * Return the set of required sources.
196 * @return Required sources.
198 Collection<SourceIdentifier> getRequiredSources() {
199 return ImmutableSet.copyOf(requiredSources);
202 SourceIdentifier getRootIdentifier() {
203 return rootIdentifier;
207 protected boolean isIgnoringIfFeatures() {
212 protected boolean isIgnoringConfig() {
217 protected boolean isParentSupportedByFeatures() {
221 void setRootIdentifierImpl(final SourceIdentifier identifier) {
222 this.rootIdentifier = requireNonNull(identifier);
225 @NonNull Registry getBehaviourRegistryImpl() {
226 return sourceContext;
229 @NonNull YangVersion getRootVersionImpl() {
230 return rootVersion == null ? DEFAULT_VERSION : rootVersion;
233 void setRootVersionImpl(final YangVersion version) {
234 checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
235 "Unsupported yang version %s in %s", version, sourceReference());
236 checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
238 this.rootVersion = requireNonNull(version);
242 * Add mutable statement to seal. Each mutable statement must be sealed
243 * as the last step of statement parser processing.
245 * @param mutableStatement
246 * mutable statement which should be sealed
248 void addMutableStmtToSeal(final MutableStatement mutableStatement) {
249 sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
252 void addRequiredSourceImpl(final SourceIdentifier dependency) {
253 checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
254 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
255 if (requiredSources.isEmpty()) {
256 requiredSources = new HashSet<>();
258 requiredSources.add(dependency);
263 StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
264 throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
268 void sweepNamespaces() {
269 LOG.trace("Sweeping root {}", this);
270 sweepNamespaces(SWEPT_NAMESPACES);