045be493784c639bcffac5043deb1d6b9f0520e3
[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 com.google.common.base.Optional;
11 import java.util.Collection;
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.meta.StmtContextUtils;
21
22 /**
23  * root statement class for a Yang source
24  */
25 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
26         StatementContextBase<A, D, E> {
27
28     private final SourceSpecificContext sourceContext;
29     private final A argument;
30
31     RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
32         super(builder);
33         this.sourceContext = sourceContext;
34         this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
35     }
36
37     RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
38         final TypeOfCopy typeOfCopy) {
39         super(original);
40
41         sourceContext = original.sourceContext;
42         this.argument = original.argument;
43
44         copyDeclaredStmts(original, newQNameModule, typeOfCopy);
45
46         copyEffectiveStmts(original, newQNameModule, typeOfCopy);
47
48     }
49
50     /**
51      * copies declared statements from original to this' substatements
52      *
53      * @param typeOfCopy
54      *            determines whether copy is used by augmentation or uses
55      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
56      */
57     private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
58             final TypeOfCopy typeOfCopy) {
59         final Collection<StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
60         for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
61             if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
62                 continue;
63             }
64             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
65         }
66     }
67
68     /**
69      * copies effective statements from original to this' substatements
70      *
71      * @param typeOfCopy
72      *            determines whether copy is used by augmentation or uses
73      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
74      */
75     private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
76             final TypeOfCopy typeOfCopy) {
77         final Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
78         for (final StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
79             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
80         }
81     }
82
83     /**
84      * @return null as root cannot have parent
85      */
86     @Override
87     public StatementContextBase<?, ?, ?> getParentContext() {
88         return null;
89     }
90
91     /**
92      * @return namespace storage of source context
93      */
94     @Override
95     public NamespaceStorageNode getParentNamespaceStorage() {
96         return sourceContext;
97     }
98
99     /**
100      * @return registry of source context
101      */
102     @Override
103     public Registry getBehaviourRegistry() {
104         return sourceContext;
105     }
106
107     @Override
108     public StorageNodeType getStorageNodeType() {
109         return StorageNodeType.ROOT_STATEMENT_LOCAL;
110     }
111     /**
112      * @return this as its own root
113      */
114     @Override
115     public RootStatementContext<?, ?, ?> getRoot() {
116         return this;
117     }
118
119     SourceSpecificContext getSourceContext() {
120         return sourceContext;
121     }
122
123     @Override
124     public A getStatementArgument() {
125         return argument;
126     }
127
128     /**
129      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
130      *
131      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
132      */
133     @Override
134     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
135             final TypeOfCopy typeOfCopy) {
136         return createCopy(null, newParent, typeOfCopy);
137     }
138
139     /**
140      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
141      *
142      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
143      */
144     @Override
145     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
146             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
147         final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
148
149         copy.addAllToCopyHistory(this.getCopyHistory());
150         copy.addToCopyHistory(typeOfCopy);
151
152         if (this.getOriginalCtx() != null) {
153             copy.setOriginalCtx(this.getOriginalCtx());
154         } else {
155             copy.setOriginalCtx(this);
156         }
157         definition().onStatementAdded(copy);
158         return copy;
159     }
160
161     @Override
162     public Optional<SchemaPath> getSchemaPath() {
163         return Optional.of(SchemaPath.ROOT);
164     }
165
166     /**
167      * @return true
168      */
169     @Override
170     public boolean isRootContext() {
171         return true;
172     }
173
174     @Override
175     public boolean isConfiguration() {
176         return true;
177     }
178
179     @Override
180     public boolean isEnabledSemanticVersioning() {
181         return sourceContext.isEnabledSemanticVersioning();
182     }
183 }