2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.BinarySpecificationImpl;
12 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.Multimap;
18 import org.opendaylight.yangtools.concepts.Mutable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
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.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ImportedNamespaceContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
35 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
36 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModuleMap;
37 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
38 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinitionMap;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.BitsSpecificationImpl;
43 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Decimal64SpecificationImpl;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.EnumSpecificationImpl;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IdentityRefSpecificationImpl;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.InstanceIdentifierSpecificationImpl;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.LeafrefSpecificationImpl;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnionSpecificationImpl;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
53 import javax.annotation.Nullable;
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.Iterator;
58 import java.util.Objects;
60 public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable {
62 public enum PhaseCompletionProgress {
68 private final StatementStreamSource source;
69 private final BuildGlobalContext currentContext;
70 private final Collection<NamespaceStorageNode> importedNamespaces = new ArrayList<>();
71 private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
73 private RootStatementContext<?, ?, ?> root;
75 private ModelProcessingPhase inProgressPhase;
76 private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
77 private QNameToStatementDefinitionMap qNameToStmtDefMap = new QNameToStatementDefinitionMap();
78 private PrefixToModuleMap prefixToModuleMap = new PrefixToModuleMap();
81 SourceSpecificContext(BuildGlobalContext currentContext, StatementStreamSource source) {
83 this.currentContext = currentContext;
86 ModelProcessingPhase getInProgressPhase() {
87 return inProgressPhase;
90 StatementDefinitionContext<?, ?, ?> getDefinition(QName name) {
91 return currentContext.getStatementDefinition(name);
94 ContextBuilder<?, ?, ?> createDeclaredChild(StatementContextBase<?, ?, ?> current, QName name, StatementSourceReference ref) {
95 StatementDefinitionContext<?, ?, ?> def = getDefinition(name);
98 //unknown-stmts (from import, include or local-scope)
99 if (qNameToStmtDefMap.get(Utils.trimPrefix(name)) != null) {
100 QName key = Utils.qNameFromArgument(current, name.getLocalName());
102 final StatementContextBase<?,?,?> extension = (StatementContextBase<?, ?, ?>) currentContext
103 .getAllFromNamespace(ExtensionNamespace.class).get(key);
104 if (extension != null) {
105 final QName qName = QName.create(((QName) ((SubstatementContext) extension).getStatementArgument())
106 .getModule().getNamespace(), ((QName) ((SubstatementContext) extension).
107 getStatementArgument()).getModule().getRevision(), extension.getIdentifier().getArgument());
109 def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
110 (getNewStatementDefinition(qName)));
112 throw new IllegalArgumentException("Not found unknown statement: " + name);
117 def = resolveTypeBodyStmts(name.getLocalName());
121 Preconditions.checkArgument(def != null, "Statement %s does not have type mapping defined.", name);
122 if (current == null) {
123 return createDeclaredRoot(def, ref);
125 return current.substatementBuilder(def, ref);
128 StatementDefinition getNewStatementDefinition(final QName qName) {
129 return new StatementDefinition() {
132 public QName getStatementName() {
138 public QName getArgumentName() {
144 public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
145 return UnknownStatementImpl.class;
150 public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
151 return UnknownEffectiveStatementImpl.class;
156 @SuppressWarnings({"rawtypes", "unchecked"})
157 private ContextBuilder<?, ?, ?> createDeclaredRoot(StatementDefinitionContext<?, ?, ?> def, StatementSourceReference ref) {
158 return new ContextBuilder(def, ref) {
161 public StatementContextBase build() throws SourceException {
163 root = new RootStatementContext(this, SourceSpecificContext.this);
165 Preconditions.checkState(root.getIdentifier().equals(createIdentifier()), "Root statement was already defined as %s.", root.getIdentifier());
174 RootStatementContext<?, ?, ?> getRoot() {
178 DeclaredStatement<?> buildDeclared() {
179 return root.buildDeclared();
182 EffectiveStatement<?, ?> buildEffective() {
183 return root.buildEffective();
186 void startPhase(final ModelProcessingPhase phase) {
187 @Nullable ModelProcessingPhase previousPhase = phase.getPreviousPhase();
188 Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
189 Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
190 inProgressPhase = phase;
194 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
195 if (ImportedNamespaceContext.class.isAssignableFrom(type)) {
196 importedNamespaces.add((NamespaceStorageNode) value);
198 getRoot().addToLocalStorage(type, key, value);
202 public StorageNodeType getStorageNodeType() {
203 return StorageNodeType.SOURCE_LOCAL_SPECIAL;
207 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
208 final V potentialLocal = getRoot().getFromLocalStorage(type, key);
209 if (potentialLocal != null) {
210 return potentialLocal;
212 for (NamespaceStorageNode importedSource : importedNamespaces) {
213 V potential = importedSource.getFromLocalStorage(type, key);
214 if (potential != null) {
223 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
224 final Map<K, V> potentialLocal = getRoot().getAllFromLocalStorage(type);
226 if (potentialLocal != null) {
227 return potentialLocal;
230 for (final NamespaceStorageNode importedSource : importedNamespaces) {
231 final Map<K, V> potential = importedSource.getAllFromLocalStorage(type);
233 if (potential != null) {
241 public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(final Class<N> type) {
242 return currentContext.getNamespaceBehaviour(type);
246 public NamespaceStorageNode getParentNamespaceStorage() {
247 return currentContext;
250 PhaseCompletionProgress tryToCompletePhase(final ModelProcessingPhase phase) throws SourceException {
251 Collection<ModifierImpl> currentPhaseModifiers = modifiers.get(phase);
253 boolean hasProgressed = hasProgress(currentPhaseModifiers);
255 boolean phaseCompleted = root.tryToCompletePhase(phase);
257 hasProgressed = (hasProgress(currentPhaseModifiers) | hasProgressed);
259 if (phaseCompleted && (currentPhaseModifiers.isEmpty())) {
260 finishedPhase = phase;
261 return PhaseCompletionProgress.FINISHED;
265 return PhaseCompletionProgress.PROGRESS;
267 return PhaseCompletionProgress.NO_PROGRESS;
271 private static boolean hasProgress(final Collection<ModifierImpl> currentPhaseModifiers) {
273 Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
274 boolean hasProgressed = false;
275 while (modifier.hasNext()) {
276 if (modifier.next().isApplied()) {
278 hasProgressed = true;
282 return hasProgressed;
286 ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
287 ModifierImpl action = new ModifierImpl(phase);
288 modifiers.put(phase, action);
293 public String toString() {
294 return "SourceSpecificContext [source=" + source + ", current=" + inProgressPhase + ", finished="
295 + finishedPhase + "]";
298 SourceException failModifiers(final ModelProcessingPhase identifier) {
299 InferenceException sourceEx = new InferenceException("Fail to infer source relationships", root.getStatementSourceReference());
302 for (ModifierImpl mod : modifiers.get(identifier)) {
305 } catch (SourceException e) {
306 sourceEx.addSuppressed(e);
312 void loadStatements() throws SourceException {
313 switch (inProgressPhase) {
315 source.writeLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef());
317 case STATEMENT_DEFINITION:
318 source.writeLinkageAndStatementDefinitions(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
320 case FULL_DECLARATION:
321 source.writeFull(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
328 private StatementDefinitionContext<?, ?, ?> resolveTypeBodyStmts(String typeArgument) {
329 switch (typeArgument) {
330 case TypeUtils.DECIMAL64:
331 return new StatementDefinitionContext<>(new Decimal64SpecificationImpl.Definition());
332 case TypeUtils.UNION:
333 return new StatementDefinitionContext<>(new UnionSpecificationImpl.Definition());
334 case TypeUtils.ENUMERATION:
335 return new StatementDefinitionContext<>(new EnumSpecificationImpl.Definition());
336 case TypeUtils.LEAF_REF:
337 return new StatementDefinitionContext<>(new LeafrefSpecificationImpl.Definition());
339 return new StatementDefinitionContext<>(new BitsSpecificationImpl.Definition());
340 case TypeUtils.IDENTITY_REF:
341 return new StatementDefinitionContext<>(new IdentityRefSpecificationImpl.Definition());
342 case TypeUtils.INSTANCE_IDENTIFIER:
343 return new StatementDefinitionContext<>(new InstanceIdentifierSpecificationImpl.Definition());
344 case TypeUtils.BINARY:
345 return new StatementDefinitionContext<>(new BinarySpecificationImpl.Definition());
352 private PrefixToModule prefixes() {
353 Map<String, QNameModule> prefixes = currentContext.getAllFromNamespace(PrefixToModule.class);
354 for (Map.Entry<String, QNameModule> prefix : prefixes.entrySet()) {
355 prefixToModuleMap.put(prefix.getKey(), prefix.getValue());
357 return prefixToModuleMap;
360 private QNameToStatementDefinition stmtDef() {
361 //regular YANG statements added
362 ImmutableMap<QName, StatementSupport<?, ?, ?>> definitions = currentContext.getSupportsForPhase(
363 inProgressPhase).getDefinitions();
364 for (Map.Entry<QName, StatementSupport<?, ?, ?>> entry : definitions.entrySet()) {
365 qNameToStmtDefMap.put(entry.getKey(), entry.getValue());
369 if (inProgressPhase.equals(ModelProcessingPhase.FULL_DECLARATION)) {
370 Map<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extensions = currentContext
371 .getAllFromNamespace(ExtensionNamespace.class);
372 if (extensions != null) {
373 for (Map.Entry<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extension : extensions
376 .put(new QName(YangConstants.RFC6020_YIN_NAMESPACE,
377 extension.getKey().getLocalName()),
378 (StatementDefinition) ((StatementContextBase<?, ?, ?>) extension
379 .getValue()).definition()
384 return qNameToStmtDefMap;