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.base.Throwables;
12 import com.google.common.collect.HashMultimap;
13 import com.google.common.collect.Multimap;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.EventListener;
18 import java.util.Iterator;
19 import java.util.LinkedHashMap;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.concepts.Identifiable;
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.model.api.meta.StatementSource;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.ValueAddedListener;
37 public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
38 NamespaceStorageSupport implements StmtContext.Mutable<A, D, E>, Identifiable<StatementIdentifier> {
40 interface OnNamespaceItemAdded extends EventListener{
42 void namespaceItemAdded(StatementContextBase<?,?,?> context, Class<?> namespace, Object key, Object value) throws SourceException;
46 interface OnPhaseFinished extends EventListener{
48 void phaseFinished(StatementContextBase<?,?,?> context, ModelProcessingPhase phase) throws SourceException;
52 interface ContextMutation {
58 abstract static class ContextBuilder<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
60 private final StatementDefinitionContext<A, D, E> definition;
61 private final StatementSourceReference stmtRef;
62 private String rawArg;
63 private StatementSourceReference argRef;
65 public ContextBuilder(StatementDefinitionContext<A, D, E> def, StatementSourceReference sourceRef) {
66 this.definition = def;
67 this.stmtRef = sourceRef;
70 public void setArgument(@Nonnull String argument, @Nonnull StatementSourceReference argumentSource) {
71 Preconditions.checkArgument(definition.hasArgument(), "Statement does not take argument.");
72 this.rawArg = Preconditions.checkNotNull(argument);
73 this.argRef = Preconditions.checkNotNull(argumentSource);
76 public String getRawArgument() {
80 public StatementSourceReference getStamementSource() {
84 public StatementSourceReference getArgumentSource() {
88 public StatementDefinitionContext<A, D, E> getDefinition() {
92 public StatementIdentifier getIdentifier() {
93 return new StatementIdentifier(definition.getStatementName(), rawArg);
96 public abstract StatementContextBase<A, D, E> build() throws SourceException;
100 private final StatementDefinitionContext<A, D, E> definition;
101 private final StatementIdentifier identifier;
102 private final StatementSourceReference statementDeclSource;
104 private LinkedHashMap<StatementIdentifier, StatementContextBase<?, ?, ?> > substatements = new LinkedHashMap<>();
106 private Collection<StatementContextBase<?, ?, ?>> declared = new ArrayList<>();
107 private Collection<StatementContextBase<?, ?, ?>> effective = new ArrayList<>();
109 private ModelProcessingPhase completedPhase;
111 private Multimap<ModelProcessingPhase,OnPhaseFinished> phaseListeners = HashMultimap.create();
112 private Multimap<ModelProcessingPhase, ContextMutation> phaseMutation = HashMultimap.create();
114 private D declaredInstance;
115 private E effectiveInstance;
118 StatementContextBase(@Nonnull ContextBuilder<A, D, E> builder) throws SourceException {
119 this.definition = builder.getDefinition();
120 this.identifier = builder.getIdentifier();
121 this.statementDeclSource = builder.getStamementSource();
122 this.completedPhase = null;
125 StatementContextBase(StatementContextBase<A,D,E> original) {
126 this.definition = original.definition;
127 this.identifier = original.identifier;
128 this.statementDeclSource = original.statementDeclSource;
129 this.completedPhase = null;
133 public abstract StatementContextBase<?, ?, ?> getParentContext();
136 public abstract RootStatementContext<?, ?, ?> getRoot();
140 public StatementIdentifier getIdentifier() {
145 public StatementSource getStatementSource() {
146 return statementDeclSource.getStatementSource();
150 public StatementSourceReference getStatementSourceReference() {
151 return statementDeclSource;
155 public String rawStatementArgument() {
156 return identifier.getArgument();
160 public Collection<StatementContextBase<?, ?, ?>> declaredSubstatements() {
161 return Collections.unmodifiableCollection(declared);
165 public Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements() {
166 return Collections.unmodifiableCollection(effective);
169 public void addEffectiveSubstatement(StatementContextBase<?, ?, ?> substatement){
170 effective.add(substatement);
173 public void addDeclaredSubstatement(StatementContextBase<?, ?, ?> substatement){
174 declared.add(substatement);
177 @SuppressWarnings({ "rawtypes", "unchecked" })
178 public ContextBuilder<?, ?, ?> substatementBuilder(StatementDefinitionContext<?, ?, ?> def,
179 StatementSourceReference ref) {
180 return new ContextBuilder(def, ref) {
183 public StatementContextBase build() throws SourceException {
184 StatementContextBase<?, ?, ?> potential = substatements.get(getIdentifier());
185 if(potential == null) {
186 potential = new SubstatementContext(StatementContextBase.this, this);
187 substatements.put(getIdentifier(), potential);
189 potential.resetLists();
190 switch (this.getStamementSource().getStatementSource()) {
192 declared.add(potential);
195 effective.add(potential);
206 public StorageNodeType getStorageNodeType() {
207 return StorageNodeType.STATEMENT_LOCAL;
211 public D buildDeclared() {
212 Preconditions.checkArgument(completedPhase == ModelProcessingPhase.FULL_DECLARATION || completedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
213 if (declaredInstance == null) {
214 declaredInstance = definition().getFactory().createDeclared(this);
216 return declaredInstance;
220 public E buildEffective() {
221 Preconditions.checkArgument(completedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
222 if (effectiveInstance == null) {
223 effectiveInstance = definition().getFactory().createEffective(this);
225 return effectiveInstance;
233 boolean tryToCompletePhase(ModelProcessingPhase phase) throws SourceException {
234 if(phase.equals(completedPhase)) {
237 Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
238 boolean finished = true;
239 while(openMutations.hasNext()) {
240 ContextMutation current = openMutations.next();
241 if(current.isFinished()) {
242 openMutations.remove();
247 for(StatementContextBase<?, ?, ?> child: declared) {
248 finished &= child.tryToCompletePhase(phase);
250 for(StatementContextBase<?, ?, ?> child: effective) {
251 finished &= child.tryToCompletePhase(phase);
254 onPhaseCompleted(phase);
261 private void onPhaseCompleted(ModelProcessingPhase phase) throws SourceException {
262 completedPhase = phase;
263 Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
264 while(listener.hasNext()) {
265 listener.next().phaseFinished(this, phase);
272 * Ends declared section of current node.
275 * @throws SourceException
278 void endDeclared(StatementSourceReference ref,ModelProcessingPhase phase) throws SourceException {
279 definition().onDeclarationFinished(this,phase);
282 protected final StatementDefinitionContext<A, D, E> definition() {
287 protected void checkLocalNamespaceAllowed(Class<? extends IdentifierNamespace<?, ?>> type) {
288 definition().checkNamespaceAllowed(type);
292 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(Class<N> type, K key, V value) {
293 //definition().onNamespaceElementAdded(this, type, key, value);
296 <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, K key, final OnNamespaceItemAdded listener) throws SourceException {
297 Object potential = getFromNamespace(type, key);
298 if(potential != null) {
299 listener.namespaceItemAdded(this, type, key, potential);
302 NamespaceBehaviour<K,V,N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
303 if(behaviour instanceof NamespaceBehaviourWithListeners) {
304 NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K,V,N>) behaviour;
305 casted.addValueListener(key, new ValueAddedListener(this) {
307 void onValueAdded(Object key, Object value) {
309 listener.namespaceItemAdded(StatementContextBase.this, type, key, value);
310 } catch (SourceException e) {
311 throw Throwables.propagate(e);
319 public StatementDefinition getPublicDefinition() {
320 return definition().getPublicView();
324 public ModelActionBuilder newInferenceAction(ModelProcessingPhase phase) {
325 return getRoot().getSourceContext().newInferenceAction(phase);
328 void addPhaseCompletedListener(ModelProcessingPhase phase, OnPhaseFinished listener) throws SourceException {
329 ModelProcessingPhase finishedPhase = completedPhase;
330 while (finishedPhase != null) {
331 if(phase.equals(finishedPhase)) {
332 listener.phaseFinished(this, finishedPhase);
335 finishedPhase = finishedPhase.getPreviousPhase();
337 phaseListeners.put(phase, listener);
340 void addMutation(ModelProcessingPhase phase, ContextMutation mutation) {
341 ModelProcessingPhase finishedPhase = completedPhase;
342 while (finishedPhase != null) {
343 if(phase.equals(finishedPhase)) {
344 throw new IllegalStateException("Mutation registered after phase was completed.");
346 finishedPhase = finishedPhase.getPreviousPhase();
348 phaseMutation.put(phase, mutation);
352 public <K,KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(
353 Class<N> namespace, KT key, StmtContext<?, ?, ?> stmt) {
354 addContextToNamespace(namespace,(K) key, stmt);