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 java.util.ArrayList;
12 import java.util.Collection;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IncludedModuleContext;
29 * root statement class for a Yang source
31 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
32 StatementContextBase<A, D, E> {
34 private final SourceSpecificContext sourceContext;
35 private final Collection<NamespaceStorageNode> includedContexts = new ArrayList<>();
36 private final A argument;
38 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
40 this.sourceContext = sourceContext;
41 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
44 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
45 final CopyType typeOfCopy) {
48 sourceContext = original.sourceContext;
49 this.argument = original.argument;
51 copyDeclaredStmts(original, newQNameModule, typeOfCopy);
53 copyEffectiveStmts(original, newQNameModule, typeOfCopy);
58 * copies declared statements from original to this' substatements
61 * determines whether copy is used by augmentation or uses
62 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
64 private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
65 final CopyType typeOfCopy) {
66 final Collection<StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
67 for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
68 if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
71 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
76 * copies effective statements from original to this' substatements
79 * determines whether copy is used by augmentation or uses
80 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
82 private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
83 final CopyType typeOfCopy) {
84 final Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
85 for (final StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
86 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
91 * @return null as root cannot have parent
94 public StatementContextBase<?, ?, ?> getParentContext() {
99 * @return namespace storage of source context
102 public NamespaceStorageNode getParentNamespaceStorage() {
103 return sourceContext;
107 * @return registry of source context
110 public Registry getBehaviourRegistry() {
111 return sourceContext;
115 public StorageNodeType getStorageNodeType() {
116 return StorageNodeType.ROOT_STATEMENT_LOCAL;
119 * @return this as its own root
122 public RootStatementContext<?, ?, ?> getRoot() {
126 SourceSpecificContext getSourceContext() {
127 return sourceContext;
131 public A getStatementArgument() {
136 * @return copy of this considering {@link CopyType} (augment, uses)
138 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
141 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
142 final CopyType typeOfCopy) {
143 return createCopy(null, newParent, typeOfCopy);
147 * @return copy of this considering {@link CopyType} (augment, uses)
149 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
152 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
153 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
154 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
156 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
158 if (this.getOriginalCtx() != null) {
159 copy.setOriginalCtx(this.getOriginalCtx());
161 copy.setOriginalCtx(this);
163 definition().onStatementAdded(copy);
168 public Optional<SchemaPath> getSchemaPath() {
169 return Optional.of(SchemaPath.ROOT);
176 public boolean isRootContext() {
181 public boolean isConfiguration() {
186 public boolean isEnabledSemanticVersioning() {
187 return sourceContext.isEnabledSemanticVersioning();
191 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
193 if (IncludedModuleContext.class.isAssignableFrom(type)) {
194 includedContexts.add((NamespaceStorageNode) value);
196 super.addToLocalStorage(type, key, value);
200 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
201 final V potentialLocal = super.getFromLocalStorage(type, key);
202 if (potentialLocal != null) {
203 return potentialLocal;
205 for (final NamespaceStorageNode includedSource : includedContexts) {
206 final V potential = includedSource.getFromLocalStorage(type, key);
207 if (potential != null) {
216 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
217 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
218 if (potentialLocal != null) {
219 return potentialLocal;
221 for (final NamespaceStorageNode includedSource : includedContexts) {
222 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
223 if (potential != null) {