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.Nullable;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
32 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
33 * of this class, hence recursive lookups from them cross this class.
35 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
36 StatementContextBase<A, D, E> {
38 private final SourceSpecificContext sourceContext;
39 private final A argument;
42 * References to RootStatementContext of submodules which are included in this source.
44 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
46 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
48 this.sourceContext = Preconditions.checkNotNull(sourceContext);
49 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
52 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
53 final CopyType typeOfCopy) {
56 sourceContext = Preconditions.checkNotNull(original.sourceContext);
57 this.argument = original.argument;
59 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
60 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
61 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
63 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
64 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
65 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
68 for (final StmtContext<?, ?, ?> stmtContext : effective) {
69 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
72 addEffectiveSubstatements(buffer);
76 * @return null as root cannot have parent
79 public StatementContextBase<?, ?, ?> getParentContext() {
84 * @return namespace storage of source context
87 public NamespaceStorageNode getParentNamespaceStorage() {
92 * @return registry of source context
95 public Registry getBehaviourRegistry() {
100 public StorageNodeType getStorageNodeType() {
101 return StorageNodeType.ROOT_STATEMENT_LOCAL;
104 * @return this as its own root
107 public RootStatementContext<?, ?, ?> getRoot() {
111 SourceSpecificContext getSourceContext() {
112 return sourceContext;
116 public A getStatementArgument() {
121 * @return copy of this considering {@link CopyType} (augment, uses)
123 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
126 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
127 final CopyType typeOfCopy) {
128 return createCopy(null, newParent, typeOfCopy);
132 * @return copy of this considering {@link CopyType} (augment, uses)
134 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
137 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
138 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
139 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
141 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
143 if (this.getOriginalCtx() != null) {
144 copy.setOriginalCtx(this.getOriginalCtx());
146 copy.setOriginalCtx(this);
148 definition().onStatementAdded(copy);
153 public Optional<SchemaPath> getSchemaPath() {
154 return Optional.of(SchemaPath.ROOT);
161 public boolean isRootContext() {
166 public boolean isConfiguration() {
171 public boolean isEnabledSemanticVersioning() {
172 return sourceContext.isEnabledSemanticVersioning();
176 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
178 if (IncludedModuleContext.class.isAssignableFrom(type)) {
179 if (includedContexts.isEmpty()) {
180 includedContexts = new ArrayList<>(1);
182 Verify.verify(value instanceof RootStatementContext);
183 includedContexts.add((RootStatementContext<?, ?, ?>) value);
185 super.addToLocalStorage(type, key, value);
189 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
190 final V potentialLocal = super.getFromLocalStorage(type, key);
191 if (potentialLocal != null) {
192 return potentialLocal;
194 for (final NamespaceStorageNode includedSource : includedContexts) {
195 final V potential = includedSource.getFromLocalStorage(type, key);
196 if (potential != null) {
205 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
206 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
207 if (potentialLocal != null) {
208 return potentialLocal;
210 for (final NamespaceStorageNode includedSource : includedContexts) {
211 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
212 if (potential != null) {