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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
33 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
34 * of this class, hence recursive lookups from them cross this class.
36 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
37 StatementContextBase<A, D, E> {
39 private final SourceSpecificContext sourceContext;
40 private final A argument;
43 * References to RootStatementContext of submodules which are included in this source.
45 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
47 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
49 this.sourceContext = Preconditions.checkNotNull(sourceContext);
50 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
53 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
54 final CopyType typeOfCopy) {
57 sourceContext = Preconditions.checkNotNull(original.sourceContext);
58 this.argument = original.argument;
60 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
61 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
62 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
64 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
65 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
66 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
69 for (final StmtContext<?, ?, ?> stmtContext : effective) {
70 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
73 addEffectiveSubstatements(buffer);
77 * @return null as root cannot have parent
80 public StatementContextBase<?, ?, ?> getParentContext() {
85 * @return namespace storage of source context
88 public NamespaceStorageNode getParentNamespaceStorage() {
93 * @return registry of source context
96 public Registry getBehaviourRegistry() {
101 public StorageNodeType getStorageNodeType() {
102 return StorageNodeType.ROOT_STATEMENT_LOCAL;
105 * @return this as its own root
109 public RootStatementContext<?, ?, ?> getRoot() {
113 SourceSpecificContext getSourceContext() {
114 return sourceContext;
118 public A getStatementArgument() {
123 * @return copy of this considering {@link CopyType} (augment, uses)
125 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
128 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
129 final CopyType typeOfCopy) {
130 return createCopy(null, newParent, typeOfCopy);
134 * @return copy of this considering {@link CopyType} (augment, uses)
136 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
139 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
140 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
141 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
143 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
145 if (this.getOriginalCtx() != null) {
146 copy.setOriginalCtx(this.getOriginalCtx());
148 copy.setOriginalCtx(this);
150 definition().onStatementAdded(copy);
156 public Optional<SchemaPath> getSchemaPath() {
157 return Optional.of(SchemaPath.ROOT);
164 public boolean isRootContext() {
169 public boolean isConfiguration() {
174 public boolean isEnabledSemanticVersioning() {
175 return sourceContext.isEnabledSemanticVersioning();
179 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
181 if (IncludedModuleContext.class.isAssignableFrom(type)) {
182 if (includedContexts.isEmpty()) {
183 includedContexts = new ArrayList<>(1);
185 Verify.verify(value instanceof RootStatementContext);
186 includedContexts.add((RootStatementContext<?, ?, ?>) value);
188 super.addToLocalStorage(type, key, value);
192 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
193 final V potentialLocal = super.getFromLocalStorage(type, key);
194 if (potentialLocal != null) {
195 return potentialLocal;
197 for (final NamespaceStorageNode includedSource : includedContexts) {
198 final V potential = includedSource.getFromLocalStorage(type, key);
199 if (potential != null) {
208 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
209 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
210 if (potentialLocal != null) {
211 return potentialLocal;
213 for (final NamespaceStorageNode includedSource : includedContexts) {
214 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
215 if (potential != null) {