BUG-5222: Reuse substatements across phases
[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 com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
33
34 /**
35  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
36  * of this class, hence recursive lookups from them cross this class.
37  */
38 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
39         StatementContextBase<A, D, E> {
40
41     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
42
43     private final SourceSpecificContext sourceContext;
44     private final A argument;
45
46     private YangVersion version;
47
48     /**
49      * References to RootStatementContext of submodules which are included in this source.
50      */
51     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
52
53     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
54         final StatementSourceReference ref, final String rawArgument) {
55         super(def, ref, rawArgument);
56         this.sourceContext = Preconditions.checkNotNull(sourceContext);
57         this.argument = def.parseArgumentValue(this, rawArgument);
58     }
59
60     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
61         final StatementSourceReference ref, final String rawArgument, final YangVersion version) {
62         this(sourceContext, def, ref, rawArgument);
63         this.setRootVersion(version);
64     }
65
66     RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
67         final CopyType typeOfCopy) {
68         super(original);
69
70         sourceContext = Preconditions.checkNotNull(original.sourceContext);
71         this.argument = original.argument;
72
73         final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
74         final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
75         final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
76
77         for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
78             if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
79                 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
80             }
81         }
82         for (final StmtContext<?, ?, ?> stmtContext : effective) {
83             buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
84         }
85
86         addEffectiveSubstatements(buffer);
87     }
88
89     /**
90      * @return null as root cannot have parent
91      */
92     @Override
93     public StatementContextBase<?, ?, ?> getParentContext() {
94         return null;
95     }
96
97     /**
98      * @return namespace storage of source context
99      */
100     @Override
101     public NamespaceStorageNode getParentNamespaceStorage() {
102         return sourceContext;
103     }
104
105     @Override
106     public Registry getBehaviourRegistry() {
107         return sourceContext;
108     }
109
110     @Override
111     public StorageNodeType getStorageNodeType() {
112         return StorageNodeType.ROOT_STATEMENT_LOCAL;
113     }
114     /**
115      * @return this as its own root
116      */
117     @Nonnull
118     @Override
119     public RootStatementContext<?, ?, ?> getRoot() {
120         return this;
121     }
122
123     SourceSpecificContext getSourceContext() {
124         return sourceContext;
125     }
126
127     @Override
128     public A getStatementArgument() {
129         return argument;
130     }
131
132     /**
133      * @return copy of this considering {@link CopyType} (augment, uses)
134      *
135      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
136      */
137     @Override
138     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
139             final CopyType typeOfCopy) {
140         return createCopy(null, newParent, typeOfCopy);
141     }
142
143     /**
144      * @return copy of this considering {@link CopyType} (augment, uses)
145      *
146      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
147      */
148     @Override
149     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
150             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
151         final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
152
153         copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
154
155         if (this.getOriginalCtx() != null) {
156             copy.setOriginalCtx(this.getOriginalCtx());
157         } else {
158             copy.setOriginalCtx(this);
159         }
160         definition().onStatementAdded(copy);
161         return copy;
162     }
163
164     @Nonnull
165     @Override
166     public Optional<SchemaPath> getSchemaPath() {
167         return Optional.of(SchemaPath.ROOT);
168     }
169
170     /**
171      * @return true
172      */
173     @Override
174     public boolean isRootContext() {
175         return true;
176     }
177
178     @Override
179     public boolean isConfiguration() {
180         return true;
181     }
182
183     @Override
184     public boolean isEnabledSemanticVersioning() {
185         return sourceContext.isEnabledSemanticVersioning();
186     }
187
188     @Override
189     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
190             final V value) {
191         if (IncludedModuleContext.class.isAssignableFrom(type)) {
192             if (includedContexts.isEmpty()) {
193                 includedContexts = new ArrayList<>(1);
194             }
195             Verify.verify(value instanceof RootStatementContext);
196             includedContexts.add((RootStatementContext<?, ?, ?>) value);
197         }
198         super.addToLocalStorage(type, key, value);
199     }
200
201     @Override
202     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
203         final V potentialLocal = super.getFromLocalStorage(type, key);
204         if (potentialLocal != null) {
205             return potentialLocal;
206         }
207         for (final NamespaceStorageNode includedSource : includedContexts) {
208             final V potential = includedSource.getFromLocalStorage(type, key);
209             if (potential != null) {
210                 return potential;
211             }
212         }
213         return null;
214     }
215
216     @Nullable
217     @Override
218     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
219         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
220         if (potentialLocal != null) {
221             return potentialLocal;
222         }
223         for (final NamespaceStorageNode includedSource : includedContexts) {
224             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
225             if (potential != null) {
226                 return potential;
227             }
228         }
229         return null;
230     }
231
232     @Override
233     public YangVersion getRootVersion() {
234         return version == null ? DEFAULT_VERSION : version;
235     }
236
237     @Override
238     public void setRootVersion(final YangVersion version) {
239         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
240                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
241         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
242                 this.version);
243         this.version = Preconditions.checkNotNull(version);
244     }
245 }