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.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 * root statement class for a Yang source
24 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
25 StatementContextBase<A, D, E> {
27 private final SourceSpecificContext sourceContext;
28 private final A argument;
30 RootStatementContext(ContextBuilder<A, D, E> builder, SourceSpecificContext sourceContext) throws SourceException {
32 this.sourceContext = sourceContext;
33 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
36 RootStatementContext(RootStatementContext<A, D, E> original, QNameModule newQNameModule, TypeOfCopy typeOfCopy)
37 throws SourceException {
40 sourceContext = original.sourceContext;
41 this.argument = original.argument;
43 copyDeclaredStmts(original, newQNameModule, typeOfCopy);
45 copyEffectiveStmts(original, newQNameModule, typeOfCopy);
50 * copies declared statements from original to this' substatements
53 * determines whether copy is used by augmentation or uses
54 * @throws SourceException
56 private void copyDeclaredStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
57 TypeOfCopy typeOfCopy) throws SourceException {
58 Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
59 for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
60 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
65 * copies effective statements from original to this' substatements
68 * determines whether copy is used by augmentation or uses
69 * @throws SourceException
71 private void copyEffectiveStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
72 TypeOfCopy typeOfCopy) throws SourceException {
73 Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
74 for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
75 this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
80 * @return null as root cannot have parent
83 public StatementContextBase<?, ?, ?> getParentContext() {
88 * @return namespace storage of source context
91 public NamespaceStorageNode getParentNamespaceStorage() {
96 * @return registry of source context
99 public Registry getBehaviourRegistry() {
100 return sourceContext;
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 TypeOfCopy} (augment, uses)
123 * @throws SourceException instance of SourceException
126 public StatementContextBase<?, ?, ?> createCopy(StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
127 throws SourceException {
128 return createCopy(null, newParent, typeOfCopy);
132 * @return copy of this considering {@link TypeOfCopy} (augment, uses)
134 * @throws SourceException instance of SourceException
137 public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule,
138 StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy) throws SourceException {
139 RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
141 copy.addAllToCopyHistory(this.getCopyHistory());
142 copy.addToCopyHistory(typeOfCopy);
144 if(this.getOriginalCtx() != null) {
145 copy.setOriginalCtx(this.getOriginalCtx());
147 copy.setOriginalCtx(this);
154 * @return this' argument as it is the only from root (this)
157 public List<Object> getArgumentsFromRoot() {
158 List<Object> argumentList = new LinkedList<>();
159 argumentList.add(argument);
164 * @return this as it is the only\context from root (this)
167 public List<StmtContext<?, ?, ?>> getStmtContextsFromRoot() {
168 List<StmtContext<?, ?, ?>> stmtContextsList = new LinkedList<>();
169 stmtContextsList.add(this);
170 return stmtContextsList;
177 public boolean isRootContext() {