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.stmt.rfc6020.IncludedModuleContext;
28 * root statement class for a Yang source
30 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
31 StatementContextBase<A, D, E> {
33 private final SourceSpecificContext sourceContext;
34 private final Collection<NamespaceStorageNode> includedContexts = new ArrayList<>();
35 private final A argument;
37 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
39 this.sourceContext = sourceContext;
40 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
43 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
44 final TypeOfCopy typeOfCopy) {
47 sourceContext = original.sourceContext;
48 this.argument = original.argument;
50 copyDeclaredStmts(original, newQNameModule, typeOfCopy);
52 copyEffectiveStmts(original, newQNameModule, typeOfCopy);
57 * copies declared statements from original to this' substatements
60 * determines whether copy is used by augmentation or uses
61 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
63 private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
64 final TypeOfCopy typeOfCopy) {
65 final Collection<StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
66 for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
67 if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
70 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
75 * copies effective statements from original to this' substatements
78 * determines whether copy is used by augmentation or uses
79 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
81 private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
82 final TypeOfCopy typeOfCopy) {
83 final Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
84 for (final StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
85 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
90 * @return null as root cannot have parent
93 public StatementContextBase<?, ?, ?> getParentContext() {
98 * @return namespace storage of source context
101 public NamespaceStorageNode getParentNamespaceStorage() {
102 return sourceContext;
106 * @return registry of source context
109 public Registry getBehaviourRegistry() {
110 return sourceContext;
114 public StorageNodeType getStorageNodeType() {
115 return StorageNodeType.ROOT_STATEMENT_LOCAL;
118 * @return this as its own root
121 public RootStatementContext<?, ?, ?> getRoot() {
125 SourceSpecificContext getSourceContext() {
126 return sourceContext;
130 public A getStatementArgument() {
135 * @return copy of this considering {@link TypeOfCopy} (augment, uses)
137 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
140 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
141 final TypeOfCopy typeOfCopy) {
142 return createCopy(null, newParent, typeOfCopy);
146 * @return copy of this considering {@link TypeOfCopy} (augment, uses)
148 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
151 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
152 final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
153 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
155 copy.addAllToCopyHistory(this.getCopyHistory());
156 copy.addToCopyHistory(typeOfCopy);
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) {