6b876c71f607d7f215d0aef2cbad582b2f521aec
[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.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;
20
21 /**
22  * root statement class for a Yang source
23  */
24 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
25         StatementContextBase<A, D, E> {
26
27     private final SourceSpecificContext sourceContext;
28     private final A argument;
29
30     RootStatementContext(ContextBuilder<A, D, E> builder, SourceSpecificContext sourceContext) throws SourceException {
31         super(builder);
32         this.sourceContext = sourceContext;
33         this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
34     }
35
36     RootStatementContext(RootStatementContext<A, D, E> original, QNameModule newQNameModule, TypeOfCopy typeOfCopy)
37             throws SourceException {
38         super(original);
39
40         sourceContext = original.sourceContext;
41         this.argument = original.argument;
42
43         copyDeclaredStmts(original, newQNameModule, typeOfCopy);
44
45         copyEffectiveStmts(original, newQNameModule, typeOfCopy);
46
47     }
48
49     /**
50      * copies declared statements from original to this' substatements
51      *
52      * @param typeOfCopy
53      *            determines whether copy is used by augmentation or uses
54      * @throws SourceException
55      */
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));
61         }
62     }
63
64     /**
65      * copies effective statements from original to this' substatements
66      *
67      * @param typeOfCopy
68      *            determines whether copy is used by augmentation or uses
69      * @throws SourceException
70      */
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));
76         }
77     }
78
79     /**
80      * @return null as root cannot have parent
81      */
82     @Override
83     public StatementContextBase<?, ?, ?> getParentContext() {
84         return null;
85     }
86
87     /**
88      * @return namespace storage of source context
89      */
90     @Override
91     public NamespaceStorageNode getParentNamespaceStorage() {
92         return sourceContext;
93     }
94
95     /**
96      * @return registry of source context
97      */
98     @Override
99     public Registry getBehaviourRegistry() {
100         return sourceContext;
101     }
102
103     /**
104      * @return this as its own root
105      */
106     @Override
107     public RootStatementContext<?, ?, ?> getRoot() {
108         return this;
109     }
110
111     SourceSpecificContext getSourceContext() {
112         return sourceContext;
113     }
114
115     @Override
116     public A getStatementArgument() {
117         return argument;
118     }
119
120     /**
121      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
122      *
123      * @throws SourceException
124      */
125     @Override
126     public StatementContextBase<?, ?, ?> createCopy(StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
127             throws SourceException {
128         return createCopy(null, newParent, typeOfCopy);
129     }
130
131     /**
132      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
133      *
134      * @throws SourceException
135      */
136     @Override
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);
140         copy.setTypeOfCopy(typeOfCopy);
141         copy.setOriginalCtx(this);
142         return copy;
143     }
144
145     /**
146      * @return this' argument as it is the only from root (this)
147      */
148     @Override
149     public List<Object> getArgumentsFromRoot() {
150         List<Object> argumentList = new LinkedList<>();
151         argumentList.add(argument);
152         return argumentList;
153     }
154
155     /**
156      * @return this as it is the only\context from root (this)
157      */
158     @Override
159     public List<StmtContext<?, ?, ?>> getStmtContextsFromRoot() {
160         List<StmtContext<?, ?, ?>> stmtContextsList = new LinkedList<>();
161         stmtContextsList.add(this);
162         return stmtContextsList;
163     }
164
165     /**
166      * @return true
167      */
168     @Override
169     public boolean isRootContext() {
170         return true;
171     }
172
173 }