Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-reactor / 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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Set;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.common.YangVersion;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
30 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
38
39 /**
40  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
41  * of this class, hence recursive lookups from them cross this class.
42  */
43 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
44         StatementContextBase<A, D, E> {
45
46     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
47
48     private final SourceSpecificContext sourceContext;
49     private final A argument;
50
51     private YangVersion rootVersion;
52     private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
53     private SourceIdentifier rootIdentifier;
54
55     /**
56      * References to RootStatementContext of submodules which are included in this source.
57      */
58     private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
59
60     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
61         final StatementSourceReference ref, final String rawArgument) {
62         super(def, ref, rawArgument);
63         this.sourceContext = requireNonNull(sourceContext);
64         this.argument = def.parseArgumentValue(this, rawStatementArgument());
65     }
66
67     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
68             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
69             final SourceIdentifier identifier) {
70         this(sourceContext, def, ref, rawArgument);
71         this.setRootVersion(version);
72         this.setRootIdentifier(identifier);
73     }
74
75     @Override
76     public StatementContextBase<?, ?, ?> getParentContext() {
77         // null as root cannot have parent
78         return null;
79     }
80
81     @Override
82     public NamespaceStorageNode getParentNamespaceStorage() {
83         // namespace storage of source context
84         return sourceContext;
85     }
86
87     @Override
88     public Registry getBehaviourRegistry() {
89         return sourceContext;
90     }
91
92     @Override
93     public StorageNodeType getStorageNodeType() {
94         return StorageNodeType.ROOT_STATEMENT_LOCAL;
95     }
96
97     @Override
98     public RootStatementContext<?, ?, ?> getRoot() {
99         // this as its own root
100         return this;
101     }
102
103     SourceSpecificContext getSourceContext() {
104         return sourceContext;
105     }
106
107     @Override
108     public A getStatementArgument() {
109         return argument;
110     }
111
112     @Override
113     public Optional<SchemaPath> getSchemaPath() {
114         return Optional.of(SchemaPath.ROOT);
115     }
116
117     @Override
118     public boolean isConfiguration() {
119         return true;
120     }
121
122     @Override
123     public boolean isEnabledSemanticVersioning() {
124         return sourceContext.isEnabledSemanticVersioning();
125     }
126
127     @Override
128     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
129             final V value) {
130         if (IncludedModuleContext.class.isAssignableFrom(type)) {
131             if (includedContexts.isEmpty()) {
132                 includedContexts = new ArrayList<>(1);
133             }
134             verify(value instanceof RootStatementContext);
135             includedContexts.add((RootStatementContext<?, ?, ?>) value);
136         }
137         return super.putToLocalStorage(type, key, value);
138     }
139
140     @Override
141     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
142         return getFromLocalStorage(type, key, new HashSet<>());
143     }
144
145     /*
146      * We need to track already checked RootStatementContexts due to possible
147      * circular chains of includes between submodules
148      */
149     private <K, V, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
150             final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
151         final V potentialLocal = super.getFromLocalStorage(type, key);
152         if (potentialLocal != null) {
153             return potentialLocal;
154         }
155
156         alreadyChecked.add(this);
157         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
158             if (alreadyChecked.contains(includedSource)) {
159                 continue;
160             }
161             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
162             if (potential != null) {
163                 return potential;
164             }
165         }
166         return null;
167     }
168
169     @Override
170     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
171         return getAllFromLocalStorage(type, new HashSet<>());
172     }
173
174     /*
175      * We need to track already checked RootStatementContexts due to possible
176      * circular chains of includes between submodules
177      */
178     private <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
179             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
180         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
181         if (potentialLocal != null) {
182             return potentialLocal;
183         }
184
185         alreadyChecked.add(this);
186         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
187             if (alreadyChecked.contains(includedSource)) {
188                 continue;
189             }
190             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
191             if (potential != null) {
192                 return potential;
193             }
194         }
195         return null;
196     }
197
198     @Override
199     public YangVersion getRootVersion() {
200         return rootVersion == null ? DEFAULT_VERSION : rootVersion;
201     }
202
203     @Override
204     public void setRootVersion(final YangVersion version) {
205         checkArgument(sourceContext.getSupportedVersions().contains(version),
206                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
207         checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
208                 this.rootVersion);
209         this.rootVersion = requireNonNull(version);
210     }
211
212     @Override
213     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
214         sourceContext.addMutableStmtToSeal(mutableStatement);
215     }
216
217     @Override
218     public void addRequiredSource(final SourceIdentifier dependency) {
219         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
220                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
221         if (requiredSources.isEmpty()) {
222             requiredSources = new HashSet<>();
223         }
224         requiredSources.add(dependency);
225     }
226
227     /**
228      * Return the set of required sources.
229      *
230      * @return Required sources.
231      */
232     Collection<SourceIdentifier> getRequiredSources() {
233         return ImmutableSet.copyOf(requiredSources);
234     }
235
236     @Override
237     public void setRootIdentifier(final SourceIdentifier identifier) {
238         this.rootIdentifier = requireNonNull(identifier);
239     }
240
241     SourceIdentifier getRootIdentifier() {
242         return rootIdentifier;
243     }
244
245     @Override
246     protected boolean isIgnoringIfFeatures() {
247         return false;
248     }
249
250     @Override
251     protected boolean isIgnoringConfig() {
252         return false;
253     }
254
255     @Override
256     protected boolean isParentSupportedByFeatures() {
257         return true;
258     }
259 }