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 com.google.common.base.Preconditions;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.Multimap;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.Objects;
21 import javax.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Mutable;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ImportedNamespaceContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
44 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
45 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
46 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModuleMap;
47 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
48 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinitionMap;
49 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
50 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
51 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.BitsSpecificationImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Decimal64SpecificationImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.EnumSpecificationImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IdentityRefSpecificationImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.InstanceIdentifierSpecificationImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.LeafrefSpecificationImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnionSpecificationImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
61 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
63 public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable {
65 public enum PhaseCompletionProgress {
71 private final StatementStreamSource source;
72 private final BuildGlobalContext currentContext;
73 private final Collection<NamespaceStorageNode> importedNamespaces = new ArrayList<>();
74 private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
76 private RootStatementContext<?, ?, ?> root;
78 private ModelProcessingPhase inProgressPhase;
79 private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
80 private final QNameToStatementDefinitionMap qNameToStmtDefMap = new QNameToStatementDefinitionMap();
81 private final PrefixToModuleMap prefixToModuleMap = new PrefixToModuleMap();
84 SourceSpecificContext(final BuildGlobalContext currentContext, final StatementStreamSource source) {
86 this.currentContext = currentContext;
89 ModelProcessingPhase getInProgressPhase() {
90 return inProgressPhase;
93 StatementDefinitionContext<?, ?, ?> getDefinition(final QName name) {
94 return currentContext.getStatementDefinition(name);
97 ContextBuilder<?, ?, ?> createDeclaredChild(final StatementContextBase<?, ?, ?> current, final QName name, final StatementSourceReference ref) {
98 StatementDefinitionContext<?, ?, ?> def = getDefinition(name);
101 //unknown-stmts (from import, include or local-scope)
102 if (qNameToStmtDefMap.get(name) != null) {
103 final StatementContextBase<?, ?, ?> extension = (StatementContextBase<?, ?, ?>) currentContext
104 .getAllFromNamespace(ExtensionNamespace.class).get(name);
105 if (extension != null) {
106 final QName arg = (QName) extension.getStatementArgument();
107 final QName qName = current.getFromNamespace(QNameCacheNamespace.class,
108 QName.create(arg, extension.getIdentifier().getArgument()));
110 def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(
111 getNewStatementDefinition(qName)));
113 throw new IllegalArgumentException("Not found unknown statement: " + name);
117 def = resolveTypeBodyStmts(name.getLocalName());
120 else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
121 QName qName = Utils.qNameFromArgument(current, name.getLocalName());
123 def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
124 (getNewStatementDefinition(qName)));
127 Preconditions.checkArgument(def != null, "Statement %s does not have type mapping defined.", name);
128 if (current == null) {
129 return createDeclaredRoot(def, ref);
131 return current.substatementBuilder(def, ref);
134 // FIXME: This should be populated differently
135 StatementDefinition getNewStatementDefinition(final QName qName) {
136 return new ModelDefinedStatementDefinition(qName);
139 @SuppressWarnings({"rawtypes", "unchecked"})
140 private ContextBuilder<?, ?, ?> createDeclaredRoot(final StatementDefinitionContext<?, ?, ?> def, final StatementSourceReference ref) {
141 return new ContextBuilder(def, ref) {
144 public StatementContextBase build() throws SourceException {
146 root = new RootStatementContext(this, SourceSpecificContext.this);
148 Preconditions.checkState(root.getIdentifier().equals(createIdentifier()), "Root statement was already defined as %s.", root.getIdentifier());
157 RootStatementContext<?, ?, ?> getRoot() {
161 DeclaredStatement<?> buildDeclared() {
162 return root.buildDeclared();
165 EffectiveStatement<?, ?> buildEffective() {
166 return root.buildEffective();
169 void startPhase(final ModelProcessingPhase phase) {
170 @Nullable ModelProcessingPhase previousPhase = phase.getPreviousPhase();
171 Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
172 Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
173 inProgressPhase = phase;
177 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
178 if (ImportedNamespaceContext.class.isAssignableFrom(type)) {
179 importedNamespaces.add((NamespaceStorageNode) value);
181 getRoot().addToLocalStorage(type, key, value);
185 public StorageNodeType getStorageNodeType() {
186 return StorageNodeType.SOURCE_LOCAL_SPECIAL;
190 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
191 final V potentialLocal = getRoot().getFromLocalStorage(type, key);
192 if (potentialLocal != null) {
193 return potentialLocal;
195 for (NamespaceStorageNode importedSource : importedNamespaces) {
196 V potential = importedSource.getFromLocalStorage(type, key);
197 if (potential != null) {
206 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
207 final Map<K, V> potentialLocal = getRoot().getAllFromLocalStorage(type);
209 if (potentialLocal != null) {
210 return potentialLocal;
213 for (final NamespaceStorageNode importedSource : importedNamespaces) {
214 final Map<K, V> potential = importedSource.getAllFromLocalStorage(type);
216 if (potential != null) {
224 public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(final Class<N> type) {
225 return currentContext.getNamespaceBehaviour(type);
229 public NamespaceStorageNode getParentNamespaceStorage() {
230 return currentContext;
233 PhaseCompletionProgress tryToCompletePhase(final ModelProcessingPhase phase) throws SourceException {
234 Collection<ModifierImpl> currentPhaseModifiers = modifiers.get(phase);
236 boolean hasProgressed = tryToProgress(currentPhaseModifiers);
238 boolean phaseCompleted = root.tryToCompletePhase(phase);
240 hasProgressed = (tryToProgress(currentPhaseModifiers) | hasProgressed);
242 if (phaseCompleted && (currentPhaseModifiers.isEmpty())) {
243 finishedPhase = phase;
244 return PhaseCompletionProgress.FINISHED;
248 return PhaseCompletionProgress.PROGRESS;
250 return PhaseCompletionProgress.NO_PROGRESS;
254 private static boolean tryToProgress(final Collection<ModifierImpl> currentPhaseModifiers) {
256 Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
257 boolean hasProgressed = false;
258 while (modifier.hasNext()) {
259 if (modifier.next().tryApply()) {
261 hasProgressed = true;
265 return hasProgressed;
269 ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
270 ModifierImpl action = new ModifierImpl(phase);
271 modifiers.put(phase, action);
276 public String toString() {
277 return "SourceSpecificContext [source=" + source + ", current=" + inProgressPhase + ", finished="
278 + finishedPhase + "]";
281 SourceException failModifiers(final ModelProcessingPhase identifier) {
282 final List<SourceException> exceptions = new ArrayList<>();
283 for (ModifierImpl mod : modifiers.get(identifier)) {
286 } catch (SourceException e) {
291 final String message = String.format("Yang model processing phase %s failed", identifier);
292 if (exceptions.isEmpty()) {
293 return new InferenceException(message, root.getStatementSourceReference());
296 final InferenceException e = new InferenceException(message, root.getStatementSourceReference(),
298 final Iterator<SourceException> it = exceptions.listIterator(1);
299 while (it.hasNext()) {
300 e.addSuppressed(it.next());
306 void loadStatements() throws SourceException {
307 switch (inProgressPhase) {
309 source.writeLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef());
311 case STATEMENT_DEFINITION:
312 source.writeLinkageAndStatementDefinitions(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
314 case FULL_DECLARATION:
315 source.writeFull(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
322 private static StatementDefinitionContext<?, ?, ?> resolveTypeBodyStmts(final String typeArgument) {
323 switch (typeArgument) {
324 case TypeUtils.DECIMAL64:
325 return new StatementDefinitionContext<>(new Decimal64SpecificationImpl.Definition());
326 case TypeUtils.UNION:
327 return new StatementDefinitionContext<>(new UnionSpecificationImpl.Definition());
328 case TypeUtils.ENUMERATION:
329 return new StatementDefinitionContext<>(new EnumSpecificationImpl.Definition());
330 case TypeUtils.LEAF_REF:
331 return new StatementDefinitionContext<>(new LeafrefSpecificationImpl.Definition());
333 return new StatementDefinitionContext<>(new BitsSpecificationImpl.Definition());
334 case TypeUtils.IDENTITY_REF:
335 return new StatementDefinitionContext<>(new IdentityRefSpecificationImpl.Definition());
336 case TypeUtils.INSTANCE_IDENTIFIER:
337 return new StatementDefinitionContext<>(new InstanceIdentifierSpecificationImpl.Definition());
343 private PrefixToModule prefixes() {
344 final Map<String, ModuleIdentifier> allPrefixes = getRoot().getAllFromNamespace(ImpPrefixToModuleIdentifier
346 final Map<String, ModuleIdentifier> belongsToPrefixes = getRoot().getAllFromNamespace
347 (BelongsToPrefixToModuleIdentifier.class);
348 if (belongsToPrefixes != null)
349 allPrefixes.putAll(belongsToPrefixes);
351 for (Entry<String, ModuleIdentifier> stringModuleIdentifierEntry : allPrefixes.entrySet()) {
352 final QNameModule namespace = getRoot().getFromNamespace(ModuleIdentifierToModuleQName.class,
353 stringModuleIdentifierEntry.getValue());
354 prefixToModuleMap.put(stringModuleIdentifierEntry.getKey(), namespace);
356 return prefixToModuleMap;
359 private QNameToStatementDefinition stmtDef() {
360 //regular YANG statements added
361 ImmutableMap<QName, StatementSupport<?, ?, ?>> definitions = currentContext.getSupportsForPhase(
362 inProgressPhase).getDefinitions();
363 for (Map.Entry<QName, StatementSupport<?, ?, ?>> entry : definitions.entrySet()) {
364 qNameToStmtDefMap.put(entry.getKey(), entry.getValue());
368 if (inProgressPhase.equals(ModelProcessingPhase.FULL_DECLARATION)) {
369 Map<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extensions = currentContext
370 .getAllFromNamespace(ExtensionNamespace.class);
371 if (extensions != null) {
372 for (Map.Entry<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extension : extensions
375 .put((extension.getKey()),
376 (StatementDefinition) ((StatementContextBase<?, ?, ?>) extension
377 .getValue()).definition()
382 return qNameToStmtDefMap;