Bug 2366 - Effective statements impl for new yang parser.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import java.util.List;
11
12 import java.util.LinkedList;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21
22 class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
23         extends StatementContextBase<A, D, E> {
24
25     private final SourceSpecificContext sourceContext;
26     private final A argument;
27     private LinkedList<?> linkedList;
28
29     RootStatementContext(ContextBuilder<A, D, E> builder,
30             SourceSpecificContext sourceContext) throws SourceException {
31         super(builder);
32         this.sourceContext = sourceContext;
33         this.argument = builder.getDefinition().parseArgumentValue(this,
34                 builder.getRawArgument());
35     }
36
37     RootStatementContext(RootStatementContext<A, D, E> original,
38             QNameModule newQNameModule) throws SourceException {
39         super(original);
40
41         sourceContext = original.sourceContext;
42         this.argument = original.argument;
43
44         copyDeclaredStmts(original, newQNameModule);
45
46         copyEffectiveStmts(original, newQNameModule);
47
48     }
49
50     private void copyDeclaredStmts(RootStatementContext<A, D, E> original,
51             QNameModule newQNameModule) throws SourceException {
52         Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original
53                 .declaredSubstatements();
54         for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
55             this.addEffectiveSubstatement(stmtContext
56                     .createCopy(newQNameModule,this));
57         }
58     }
59
60     private void copyEffectiveStmts(RootStatementContext<A, D, E> original,
61             QNameModule newQNameModule) throws SourceException {
62         Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original
63                 .effectiveSubstatements();
64         for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
65             this.addEffectiveSubstatement(stmtContext
66                     .createCopy(newQNameModule,this));
67         }
68     }
69
70     @Override
71     public StatementContextBase<?, ?, ?> getParentContext() {
72         return null;
73     }
74
75     @Override
76     public NamespaceStorageNode getParentNamespaceStorage() {
77         return sourceContext;
78     }
79
80     @Override
81     public Registry getBehaviourRegistry() {
82         return sourceContext;
83     }
84
85     @Override
86     public RootStatementContext<?, ?, ?> getRoot() {
87         return this;
88     }
89
90     SourceSpecificContext getSourceContext() {
91         return sourceContext;
92     }
93
94     @Override
95     public A getStatementArgument() {
96         return argument;
97     }
98
99     @Override
100     public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent)
101             throws SourceException {
102
103         StatementContextBase<A, D, E> copy = new RootStatementContext<A, D, E>(
104                 this, newQNameModule);
105         return copy;
106     }
107
108     @Override
109     public List<Object> getArgumentsFromRoot() {
110         LinkedList<Object> argumentList = new LinkedList<Object>();
111         argumentList.add(argument);
112         return argumentList;
113     }
114
115 }