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(Utils.trimPrefix(name)) != null) {
103 QName key = Utils.qNameFromArgument(current, name.getLocalName());
105 final StatementContextBase<?,?,?> extension = (StatementContextBase<?, ?, ?>) currentContext
106 .getAllFromNamespace(ExtensionNamespace.class).get(key);
107 if (extension != null) {
108 final QName arg = (QName) extension.getStatementArgument();
109 final QName qName = current.getFromNamespace(QNameCacheNamespace.class,
110 QName.create(arg, extension.getIdentifier().getArgument()));
112 def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
113 (getNewStatementDefinition(qName)));
115 throw new IllegalArgumentException("Not found unknown statement: " + name);
120 def = resolveTypeBodyStmts(name.getLocalName());
123 else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
124 QName qName = Utils.qNameFromArgument(current, name.getLocalName());
126 def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
127 (getNewStatementDefinition(qName)));
130 Preconditions.checkArgument(def != null, "Statement %s does not have type mapping defined.", name);
131 if (current == null) {
132 return createDeclaredRoot(def, ref);
134 return current.substatementBuilder(def, ref);
137 // FIXME: This should be populated differently
138 StatementDefinition getNewStatementDefinition(final QName qName) {
139 return new ModelDefinedStatementDefinition(qName);
142 @SuppressWarnings({"rawtypes", "unchecked"})
143 private ContextBuilder<?, ?, ?> createDeclaredRoot(final StatementDefinitionContext<?, ?, ?> def, final StatementSourceReference ref) {
144 return new ContextBuilder(def, ref) {
147 public StatementContextBase build() throws SourceException {
149 root = new RootStatementContext(this, SourceSpecificContext.this);
151 Preconditions.checkState(root.getIdentifier().equals(createIdentifier()), "Root statement was already defined as %s.", root.getIdentifier());
160 RootStatementContext<?, ?, ?> getRoot() {
164 DeclaredStatement<?> buildDeclared() {
165 return root.buildDeclared();
168 EffectiveStatement<?, ?> buildEffective() {
169 return root.buildEffective();
172 void startPhase(final ModelProcessingPhase phase) {
173 @Nullable ModelProcessingPhase previousPhase = phase.getPreviousPhase();
174 Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
175 Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
176 inProgressPhase = phase;
180 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
181 if (ImportedNamespaceContext.class.isAssignableFrom(type)) {
182 importedNamespaces.add((NamespaceStorageNode) value);
184 getRoot().addToLocalStorage(type, key, value);
188 public StorageNodeType getStorageNodeType() {
189 return StorageNodeType.SOURCE_LOCAL_SPECIAL;
193 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
194 final V potentialLocal = getRoot().getFromLocalStorage(type, key);
195 if (potentialLocal != null) {
196 return potentialLocal;
198 for (NamespaceStorageNode importedSource : importedNamespaces) {
199 V potential = importedSource.getFromLocalStorage(type, key);
200 if (potential != null) {
209 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
210 final Map<K, V> potentialLocal = getRoot().getAllFromLocalStorage(type);
212 if (potentialLocal != null) {
213 return potentialLocal;
216 for (final NamespaceStorageNode importedSource : importedNamespaces) {
217 final Map<K, V> potential = importedSource.getAllFromLocalStorage(type);
219 if (potential != null) {
227 public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(final Class<N> type) {
228 return currentContext.getNamespaceBehaviour(type);
232 public NamespaceStorageNode getParentNamespaceStorage() {
233 return currentContext;
236 PhaseCompletionProgress tryToCompletePhase(final ModelProcessingPhase phase) throws SourceException {
237 Collection<ModifierImpl> currentPhaseModifiers = modifiers.get(phase);
239 boolean hasProgressed = tryToProgress(currentPhaseModifiers);
241 boolean phaseCompleted = root.tryToCompletePhase(phase);
243 hasProgressed = (tryToProgress(currentPhaseModifiers) | hasProgressed);
245 if (phaseCompleted && (currentPhaseModifiers.isEmpty())) {
246 finishedPhase = phase;
247 return PhaseCompletionProgress.FINISHED;
251 return PhaseCompletionProgress.PROGRESS;
253 return PhaseCompletionProgress.NO_PROGRESS;
257 private static boolean tryToProgress(final Collection<ModifierImpl> currentPhaseModifiers) {
259 Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
260 boolean hasProgressed = false;
261 while (modifier.hasNext()) {
262 if (modifier.next().tryApply()) {
264 hasProgressed = true;
268 return hasProgressed;
272 ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
273 ModifierImpl action = new ModifierImpl(phase);
274 modifiers.put(phase, action);
279 public String toString() {
280 return "SourceSpecificContext [source=" + source + ", current=" + inProgressPhase + ", finished="
281 + finishedPhase + "]";
284 SourceException failModifiers(final ModelProcessingPhase identifier) {
285 final List<SourceException> exceptions = new ArrayList<>();
286 for (ModifierImpl mod : modifiers.get(identifier)) {
289 } catch (SourceException e) {
294 final String message = String.format("Yang model processing phase %s failed", identifier);
295 if (exceptions.isEmpty()) {
296 return new InferenceException(message, root.getStatementSourceReference());
299 final InferenceException e = new InferenceException(message, root.getStatementSourceReference(),
301 final Iterator<SourceException> it = exceptions.listIterator(1);
302 while (it.hasNext()) {
303 e.addSuppressed(it.next());
309 void loadStatements() throws SourceException {
310 switch (inProgressPhase) {
312 source.writeLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef());
314 case STATEMENT_DEFINITION:
315 source.writeLinkageAndStatementDefinitions(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
317 case FULL_DECLARATION:
318 source.writeFull(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes());
325 private static StatementDefinitionContext<?, ?, ?> resolveTypeBodyStmts(final String typeArgument) {
326 switch (typeArgument) {
327 case TypeUtils.DECIMAL64:
328 return new StatementDefinitionContext<>(new Decimal64SpecificationImpl.Definition());
329 case TypeUtils.UNION:
330 return new StatementDefinitionContext<>(new UnionSpecificationImpl.Definition());
331 case TypeUtils.ENUMERATION:
332 return new StatementDefinitionContext<>(new EnumSpecificationImpl.Definition());
333 case TypeUtils.LEAF_REF:
334 return new StatementDefinitionContext<>(new LeafrefSpecificationImpl.Definition());
336 return new StatementDefinitionContext<>(new BitsSpecificationImpl.Definition());
337 case TypeUtils.IDENTITY_REF:
338 return new StatementDefinitionContext<>(new IdentityRefSpecificationImpl.Definition());
339 case TypeUtils.INSTANCE_IDENTIFIER:
340 return new StatementDefinitionContext<>(new InstanceIdentifierSpecificationImpl.Definition());
346 private PrefixToModule prefixes() {
347 final Map<String, ModuleIdentifier> allPrefixes = getRoot().getAllFromNamespace(ImpPrefixToModuleIdentifier
349 final Map<String, ModuleIdentifier> belongsToPrefixes = getRoot().getAllFromNamespace
350 (BelongsToPrefixToModuleIdentifier.class);
351 if (belongsToPrefixes != null)
352 allPrefixes.putAll(belongsToPrefixes);
354 for (Entry<String, ModuleIdentifier> stringModuleIdentifierEntry : allPrefixes.entrySet()) {
355 final QNameModule namespace = getRoot().getFromNamespace(ModuleIdentifierToModuleQName.class,
356 stringModuleIdentifierEntry.getValue());
357 prefixToModuleMap.put(stringModuleIdentifierEntry.getKey(), namespace);
359 return prefixToModuleMap;
362 private QNameToStatementDefinition stmtDef() {
363 //regular YANG statements added
364 ImmutableMap<QName, StatementSupport<?, ?, ?>> definitions = currentContext.getSupportsForPhase(
365 inProgressPhase).getDefinitions();
366 for (Map.Entry<QName, StatementSupport<?, ?, ?>> entry : definitions.entrySet()) {
367 qNameToStmtDefMap.put(entry.getKey(), entry.getValue());
371 if (inProgressPhase.equals(ModelProcessingPhase.FULL_DECLARATION)) {
372 Map<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extensions = currentContext
373 .getAllFromNamespace(ExtensionNamespace.class);
374 if (extensions != null) {
375 for (Map.Entry<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extension : extensions
378 .put((extension.getKey()),
379 (StatementDefinition) ((StatementContextBase<?, ?, ?>) extension
380 .getValue()).definition()
385 return qNameToStmtDefMap;