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 java.util.Collection;
11 import java.util.LinkedList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 * root statement class for a Yang source
25 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
26 StatementContextBase<A, D, E> {
28 private final SourceSpecificContext sourceContext;
29 private final A argument;
31 RootStatementContext(ContextBuilder<A, D, E> builder, SourceSpecificContext sourceContext) throws SourceException {
33 this.sourceContext = sourceContext;
34 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
37 RootStatementContext(RootStatementContext<A, D, E> original, QNameModule newQNameModule, TypeOfCopy typeOfCopy)
38 throws SourceException {
41 sourceContext = original.sourceContext;
42 this.argument = original.argument;
44 copyDeclaredStmts(original, newQNameModule, typeOfCopy);
46 copyEffectiveStmts(original, newQNameModule, typeOfCopy);
51 * copies declared statements from original to this' substatements
54 * determines whether copy is used by augmentation or uses
55 * @throws SourceException
57 private void copyDeclaredStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
58 TypeOfCopy typeOfCopy) throws SourceException {
59 Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
60 for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
61 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
66 * copies effective statements from original to this' substatements
69 * determines whether copy is used by augmentation or uses
70 * @throws SourceException
72 private void copyEffectiveStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
73 TypeOfCopy typeOfCopy) throws SourceException {
74 Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
75 for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
76 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
81 * @return null as root cannot have parent
84 public StatementContextBase<?, ?, ?> getParentContext() {
89 * @return namespace storage of source context
92 public NamespaceStorageNode getParentNamespaceStorage() {
97 * @return registry of source context
100 public Registry getBehaviourRegistry() {
101 return sourceContext;
105 public StorageNodeType getStorageNodeType() {
106 return StorageNodeType.ROOT_STATEMENT_LOCAL;
109 * @return this as its own root
112 public RootStatementContext<?, ?, ?> getRoot() {
116 SourceSpecificContext getSourceContext() {
117 return sourceContext;
121 public A getStatementArgument() {
126 * @return copy of this considering {@link TypeOfCopy} (augment, uses)
128 * @throws SourceException instance of SourceException
131 public StatementContextBase<?, ?, ?> createCopy(StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
132 throws SourceException {
133 return createCopy(null, newParent, typeOfCopy);
137 * @return copy of this considering {@link TypeOfCopy} (augment, uses)
139 * @throws SourceException instance of SourceException
142 public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule,
143 StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy) throws SourceException {
144 RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
146 copy.addAllToCopyHistory(this.getCopyHistory());
147 copy.addToCopyHistory(typeOfCopy);
149 if(this.getOriginalCtx() != null) {
150 copy.setOriginalCtx(this.getOriginalCtx());
152 copy.setOriginalCtx(this);
154 definition().onStatementAdded(copy);
159 * @return this' argument as it is the only from root (this)
162 public List<Object> getArgumentsFromRoot() {
163 List<Object> argumentList = new LinkedList<>();
164 argumentList.add(argument);
169 * @return this as it is the only\context from root (this)
172 public List<StmtContext<?, ?, ?>> getStmtContextsFromRoot() {
173 List<StmtContext<?, ?, ?>> stmtContextsList = new LinkedList<>();
174 stmtContextsList.add(this);
175 return stmtContextsList;
182 public boolean isRootContext() {