Bug 4662: Introduce a SemanticVersion concept - import processing
[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
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(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
31         super(builder);
32         this.sourceContext = sourceContext;
33         this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
34     }
35
36     RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
37         final TypeOfCopy typeOfCopy) {
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 org.opendaylight.yangtools.yang.parser.spi.source.SourceException
55      */
56     private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
57             final TypeOfCopy typeOfCopy) {
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 org.opendaylight.yangtools.yang.parser.spi.source.SourceException
70      */
71     private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
72             final TypeOfCopy typeOfCopy) {
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     @Override
104     public StorageNodeType getStorageNodeType() {
105         return StorageNodeType.ROOT_STATEMENT_LOCAL;
106     }
107     /**
108      * @return this as its own root
109      */
110     @Override
111     public RootStatementContext<?, ?, ?> getRoot() {
112         return this;
113     }
114
115     SourceSpecificContext getSourceContext() {
116         return sourceContext;
117     }
118
119     @Override
120     public A getStatementArgument() {
121         return argument;
122     }
123
124     /**
125      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
126      *
127      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
128      */
129     @Override
130     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
131             final TypeOfCopy typeOfCopy) {
132         return createCopy(null, newParent, typeOfCopy);
133     }
134
135     /**
136      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
137      *
138      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
139      */
140     @Override
141     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
142             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
143         RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
144
145         copy.addAllToCopyHistory(this.getCopyHistory());
146         copy.addToCopyHistory(typeOfCopy);
147
148         if (this.getOriginalCtx() != null) {
149             copy.setOriginalCtx(this.getOriginalCtx());
150         } else {
151             copy.setOriginalCtx(this);
152         }
153         definition().onStatementAdded(copy);
154         return copy;
155     }
156
157     @Override
158     public Optional<SchemaPath> getSchemaPath() {
159         return Optional.of(SchemaPath.ROOT);
160     }
161
162     /**
163      * @return true
164      */
165     @Override
166     public boolean isRootContext() {
167         return true;
168     }
169
170     @Override
171     public boolean isConfiguration() {
172         return true;
173     }
174
175     @Override
176     public boolean isEnabledSemanticVersioning() {
177         return sourceContext.isEnabledSemanticVersioning();
178     }
179 }