Remove appendCopyHistory from public view
[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 com.google.common.collect.ImmutableSet;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.Map;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
37
38 /**
39  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
40  * of this class, hence recursive lookups from them cross this class.
41  */
42 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
43         StatementContextBase<A, D, E> {
44
45     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
46
47     private final SourceSpecificContext sourceContext;
48     private final A argument;
49
50     private YangVersion version;
51     private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
52     private ModuleIdentifier identifier;
53
54     /**
55      * References to RootStatementContext of submodules which are included in this source.
56      */
57     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
58
59     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
60         final StatementSourceReference ref, final String rawArgument) {
61         super(def, ref, rawArgument);
62         this.sourceContext = Preconditions.checkNotNull(sourceContext);
63         this.argument = def.parseArgumentValue(this, rawStatementArgument());
64     }
65
66     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
67             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
68             final ModuleIdentifier identifier) {
69         this(sourceContext, def, ref, rawArgument);
70         this.setRootVersion(version);
71         this.setRootIdentifier(identifier);
72     }
73
74     private RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
75         final CopyType typeOfCopy) {
76         super(original);
77
78         sourceContext = Preconditions.checkNotNull(original.sourceContext);
79         this.argument = original.argument;
80
81         final Collection<? extends Mutable<?, ?, ?>> declared = original.mutableDeclaredSubstatements();
82         final Collection<? extends Mutable<?, ?, ?>> effective = original.mutableEffectiveSubstatements();
83         final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
84
85         for (final Mutable<?, ?, ?> stmtContext : declared) {
86             if (stmtContext.isSupportedByFeatures()) {
87                 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
88             }
89         }
90         for (final StmtContext<?, ?, ?> stmtContext : effective) {
91             buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
92         }
93
94         addEffectiveSubstatements(buffer);
95     }
96
97     /**
98      * @return null as root cannot have parent
99      */
100     @Override
101     public StatementContextBase<?, ?, ?> getParentContext() {
102         return null;
103     }
104
105     /**
106      * @return namespace storage of source context
107      */
108     @Override
109     public NamespaceStorageNode getParentNamespaceStorage() {
110         return sourceContext;
111     }
112
113     @Override
114     public Registry getBehaviourRegistry() {
115         return sourceContext;
116     }
117
118     @Override
119     public StorageNodeType getStorageNodeType() {
120         return StorageNodeType.ROOT_STATEMENT_LOCAL;
121     }
122     /**
123      * @return this as its own root
124      */
125     @Nonnull
126     @Override
127     public RootStatementContext<?, ?, ?> getRoot() {
128         return this;
129     }
130
131     SourceSpecificContext getSourceContext() {
132         return sourceContext;
133     }
134
135     @Override
136     public A getStatementArgument() {
137         return argument;
138     }
139
140     /**
141      * @return copy of this considering {@link CopyType} (augment, uses)
142      *
143      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
144      */
145     @Override
146     public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
147             final CopyType typeOfCopy) {
148         return createCopy(null, newParent, typeOfCopy);
149     }
150
151     /**
152      * @return copy of this considering {@link CopyType} (augment, uses)
153      *
154      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
155      */
156     @Override
157     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
158             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
159         final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
160
161         copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
162
163         if (this.getOriginalCtx() != null) {
164             copy.setOriginalCtx(this.getOriginalCtx());
165         } else {
166             copy.setOriginalCtx(this);
167         }
168         definition().onStatementAdded(copy);
169         return copy;
170     }
171
172     @Nonnull
173     @Override
174     public Optional<SchemaPath> getSchemaPath() {
175         return Optional.of(SchemaPath.ROOT);
176     }
177
178     /**
179      * @return true
180      */
181     @Override
182     public boolean isRootContext() {
183         return true;
184     }
185
186     @Override
187     public boolean isConfiguration() {
188         return true;
189     }
190
191     @Override
192     public boolean isInYangDataExtensionBody() {
193         return false;
194     }
195
196     @Override
197     public boolean isEnabledSemanticVersioning() {
198         return sourceContext.isEnabledSemanticVersioning();
199     }
200
201     @Override
202     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
203             final V value) {
204         if (IncludedModuleContext.class.isAssignableFrom(type)) {
205             if (includedContexts.isEmpty()) {
206                 includedContexts = new ArrayList<>(1);
207             }
208             Verify.verify(value instanceof RootStatementContext);
209             includedContexts.add((RootStatementContext<?, ?, ?>) value);
210         }
211         return super.putToLocalStorage(type, key, value);
212     }
213
214     @Nullable
215     @Override
216     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
217         return getFromLocalStorage(type, key, new HashSet<>());
218     }
219
220     /*
221      * We need to track already checked RootStatementContexts due to possible
222      * circular chains of includes between submodules
223      */
224     @Nullable
225     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
226             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
227         final V potentialLocal = super.getFromLocalStorage(type, key);
228         if (potentialLocal != null) {
229             return potentialLocal;
230         }
231
232         alreadyChecked.add(this);
233         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
234             if (alreadyChecked.contains(includedSource)) {
235                 continue;
236             }
237             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
238             if (potential != null) {
239                 return potential;
240             }
241         }
242         return null;
243     }
244
245     @Nullable
246     @Override
247     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
248         return getAllFromLocalStorage(type, new HashSet<>());
249     }
250
251     /*
252      * We need to track already checked RootStatementContexts due to possible
253      * circular chains of includes between submodules
254      */
255     @Nullable
256     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
257             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
258         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
259         if (potentialLocal != null) {
260             return potentialLocal;
261         }
262
263         alreadyChecked.add(this);
264         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
265             if (alreadyChecked.contains(includedSource)) {
266                 continue;
267             }
268             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
269             if (potential != null) {
270                 return potential;
271             }
272         }
273         return null;
274     }
275
276     @Override
277     public YangVersion getRootVersion() {
278         return version == null ? DEFAULT_VERSION : version;
279     }
280
281     @Override
282     public void setRootVersion(final YangVersion version) {
283         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
284                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
285         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
286                 this.version);
287         this.version = Preconditions.checkNotNull(version);
288     }
289
290     @Override
291     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
292         sourceContext.addMutableStmtToSeal(mutableStatement);
293     }
294
295     @Override
296     public void addRequiredModule(final ModuleIdentifier dependency) {
297         Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
298                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
299         if (requiredModules.isEmpty()) {
300             requiredModules = new HashSet<>();
301         }
302         requiredModules.add(dependency);
303     }
304
305     Collection<ModuleIdentifier> getRequiredModules() {
306         return ImmutableSet.copyOf(requiredModules);
307     }
308
309     @Override
310     public void setRootIdentifier(final ModuleIdentifier identifier) {
311         Preconditions.checkNotNull(identifier);
312         this.identifier = identifier;
313     }
314
315     ModuleIdentifier getRootIdentifier() {
316         return identifier;
317     }
318 }