2 * Copyright (c) 2015, 2016 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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
14 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL;
15 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.FULL_DECLARATION;
17 import com.google.common.base.MoreObjects;
18 import com.google.common.base.MoreObjects.ToStringHelper;
19 import java.util.HashSet;
20 import java.util.Iterator;
22 import java.util.function.Function;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
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.NamespaceKeyCriterion;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.ContextMutation;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.OnNamespaceItemAdded;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.OnPhaseFinished;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 final class ModifierImpl implements ModelActionBuilder {
40 private static final Logger LOG = LoggerFactory.getLogger(ModifierImpl.class);
42 private final InferenceContext ctx = new InferenceContext() { };
44 private final Set<AbstractPrerequisite<?>> unsatisfied = new HashSet<>(1);
45 private final Set<AbstractPrerequisite<?>> mutations = new HashSet<>(1);
47 private InferenceAction action;
48 private boolean actionApplied = false;
50 private <D> AbstractPrerequisite<D> addReq(final AbstractPrerequisite<D> prereq) {
51 LOG.trace("Modifier {} adding prerequisite {}", this, prereq);
52 unsatisfied.add(prereq);
56 private <T> @NonNull AbstractPrerequisite<T> addMutation(final @NonNull AbstractPrerequisite<T> mutation) {
57 LOG.trace("Modifier {} adding mutation {}", this, mutation);
58 mutations.add(mutation);
62 private void checkNotRegistered() {
63 checkState(action == null, "Action was already registered.");
66 private boolean removeSatisfied() {
67 final Iterator<AbstractPrerequisite<?>> it = unsatisfied.iterator();
68 while (it.hasNext()) {
69 final AbstractPrerequisite<?> prereq = it.next();
70 if (prereq.isDone()) {
71 // We are removing current prerequisite from list.
72 LOG.trace("Modifier {} prerequisite {} satisfied", this, prereq);
76 return unsatisfied.isEmpty();
85 action.prerequisiteFailed(unsatisfied);
89 private <K, C extends StmtContext<?, ?, ?>, N extends StatementNamespace<K, ?, ?>> @NonNull AbstractPrerequisite<C>
90 requiresCtxImpl(final StmtContext<?, ?, ?> context, final Class<N> namespace, final K key,
91 final ModelProcessingPhase phase) {
94 AddedToNamespace<C> addedToNs = new AddedToNamespace<>(phase);
96 contextImpl(context).onNamespaceItemAddedAction(namespace, key, addedToNs);
100 private <K, C extends StmtContext<?, ?, ?>, N extends StatementNamespace<K, ?, ?>> @NonNull AbstractPrerequisite<C>
101 requiresCtxImpl(final StmtContext<?, ?, ?> context, final Class<N> namespace,
102 final NamespaceKeyCriterion<K> criterion, final ModelProcessingPhase phase) {
103 checkNotRegistered();
105 AddedToNamespace<C> addedToNs = new AddedToNamespace<>(phase);
107 contextImpl(context).onNamespaceItemAddedAction(namespace, phase, criterion, addedToNs);
111 private <C extends StmtContext<?, ?, ?>> @NonNull AbstractPrerequisite<C> requiresCtxImpl(final C context,
112 final ModelProcessingPhase phase) {
113 checkNotRegistered();
115 PhaseFinished<C> phaseFin = new PhaseFinished<>();
117 contextImpl(context).addPhaseCompletedListener(phase, phaseFin);
121 @SuppressWarnings({ "rawtypes", "unchecked" })
122 private <K, C extends Mutable<?, ?, ?>, N extends IdentifierNamespace<K, ? extends StmtContext<?, ?, ?>>>
123 AbstractPrerequisite<C> mutatesCtxImpl(final StmtContext<?, ?, ?> context, final Class<N> namespace,
124 final K key, final ModelProcessingPhase phase) {
125 checkNotRegistered();
127 final PhaseModificationInNamespace<C> mod = new PhaseModificationInNamespace<>(EFFECTIVE_MODEL);
130 contextImpl(context).onNamespaceItemAddedAction((Class) namespace, key, mod);
134 private static StatementContextBase<?, ?, ?> contextImpl(final Object value) {
135 checkArgument(value instanceof StatementContextBase, "Supplied context %s is not provided by this reactor.",
137 return StatementContextBase.class.cast(value);
141 checkState(action != null, "Action was not defined yet.");
143 if (removeSatisfied()) {
144 if (!actionApplied) {
146 actionApplied = true;
154 public <C extends Mutable<?, ?, ?>, T extends C> Prerequisite<C> mutatesCtx(final T context,
155 final ModelProcessingPhase phase) {
156 return addMutation(new PhaseMutation<>(contextImpl(context), phase));
160 public <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
161 AbstractPrerequisite<StmtContext<A, D, E>> requiresCtx(final StmtContext<A, D, E> context,
162 final ModelProcessingPhase phase) {
163 return requiresCtxImpl(context, phase);
168 public <K, N extends StatementNamespace<K, ?, ?>> Prerequisite<StmtContext<?, ?, ?>> requiresCtx(
169 final StmtContext<?, ?, ?> context, final Class<@NonNull N> namespace, final K key,
170 final ModelProcessingPhase phase) {
171 return requiresCtxImpl(context, namespace, key, phase);
175 public <K, N extends StatementNamespace<K, ?, ?>> Prerequisite<StmtContext<?, ?, ?>> requiresCtx(
176 final StmtContext<?, ?, ?> context, final Class<@NonNull N> namespace,
177 final NamespaceKeyCriterion<K> criterion, final ModelProcessingPhase phase) {
178 return requiresCtxImpl(context, namespace, criterion, phase);
182 public <D extends DeclaredStatement<?>> Prerequisite<D> requiresDeclared(
183 final StmtContext<?, ? extends D, ?> context) {
184 return requiresCtxImpl(context, FULL_DECLARATION).transform(StmtContext::buildDeclared);
189 public <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>> Prerequisite<D>
190 requiresDeclared(final StmtContext<?, ?, ?> context, final Class<N> namespace, final K key) {
191 final AbstractPrerequisite<StmtContext<?, D, ?>> rawContext = requiresCtxImpl(context, namespace, key,
193 return rawContext.transform(StmtContext::buildDeclared);
198 public <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>>
199 AbstractPrerequisite<StmtContext<?, D, ?>> requiresDeclaredCtx(final StmtContext<?, ?, ?> context,
200 final Class<N> namespace, final K key) {
201 return requiresCtxImpl(context, namespace, key, FULL_DECLARATION);
206 public <E extends EffectiveStatement<?, ?>> Prerequisite<E> requiresEffective(
207 final StmtContext<?, ?, ? extends E> stmt) {
208 return requiresCtxImpl(stmt, EFFECTIVE_MODEL).transform(StmtContext::buildEffective);
213 public <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>> Prerequisite<E>
214 requiresEffective(final StmtContext<?, ?, ?> context, final Class<N> namespace, final K key) {
215 final AbstractPrerequisite<StmtContext<?, ?, E>> rawContext = requiresCtxImpl(context, namespace, key,
217 return rawContext.transform(StmtContext::buildEffective);
222 public <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>>
223 AbstractPrerequisite<StmtContext<?, ?, E>> requiresEffectiveCtx(final StmtContext<?, ?, ?> context,
224 final Class<N> namespace, final K key) {
225 return requiresCtxImpl(contextImpl(context), namespace, key, EFFECTIVE_MODEL);
230 public <N extends IdentifierNamespace<?, ?>> Prerequisite<Mutable<?, ?, ?>> mutatesNs(
231 final Mutable<?, ?, ?> context, final Class<N> namespace) {
232 return addMutation(new NamespaceMutation<>(contextImpl(context), namespace));
236 public <K, E extends EffectiveStatement<?, ?>, N extends IdentifierNamespace<K, ? extends StmtContext<?, ?, ?>>>
237 AbstractPrerequisite<Mutable<?, ?, E>> mutatesEffectiveCtx(final StmtContext<?, ?, ?> context,
238 final Class<N> namespace, final K key) {
239 return mutatesCtxImpl(context, namespace, key, EFFECTIVE_MODEL);
243 public <K, E extends EffectiveStatement<?, ?>, N extends IdentifierNamespace<K, ? extends StmtContext<?, ?, ?>>>
244 AbstractPrerequisite<Mutable<?, ?, E>> mutatesEffectiveCtxPath(final StmtContext<?, ?, ?> context,
245 final Class<N> namespace, final Iterable<K> keys) {
246 checkNotRegistered();
248 final PhaseModificationInNamespacePath<Mutable<?, ?, E>, K, N> ret = new PhaseModificationInNamespacePath<>(
249 EFFECTIVE_MODEL, keys);
253 ret.hookOnto(context, namespace);
258 @SuppressWarnings("checkstyle:hiddenField")
259 public void apply(final InferenceAction action) {
260 checkState(this.action == null, "Action already defined to %s", this.action);
261 this.action = requireNonNull(action);
264 private abstract class AbstractPrerequisite<T> implements Prerequisite<T> {
265 private boolean done = false;
269 @SuppressWarnings("checkstyle:hiddenField")
270 public final T resolve(final InferenceContext ctx) {
272 checkArgument(ctx == ModifierImpl.this.ctx);
273 return verifyNotNull(value, "Attempted to access unavailable prerequisite %s", this);
276 final boolean isDone() {
280 @SuppressWarnings("checkstyle:hiddenField")
281 final boolean resolvePrereq(final T value) {
287 final <O> @NonNull Prerequisite<O> transform(final Function<? super T, O> transformation) {
288 return context -> transformation.apply(resolve(context));
292 public final String toString() {
293 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
296 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
297 return toStringHelper.add("value", value);
301 private final class PhaseMutation<C> extends AbstractPrerequisite<C> implements ContextMutation {
302 @SuppressWarnings("unchecked")
303 PhaseMutation(final StatementContextBase<?, ?, ?> context, final ModelProcessingPhase phase) {
304 context.addMutation(phase, this);
305 resolvePrereq((C) context);
309 public boolean isFinished() {
314 private final class PhaseFinished<C extends StmtContext<?, ?, ?>> extends AbstractPrerequisite<C>
315 implements OnPhaseFinished {
316 @SuppressWarnings("unchecked")
318 public boolean phaseFinished(final StatementContextBase<?, ?, ?> context,
319 final ModelProcessingPhase finishedPhase) {
320 return resolvePrereq((C) context) || tryApply();
324 private final class NamespaceMutation<N extends IdentifierNamespace<?, ?>>
325 extends AbstractPrerequisite<Mutable<?, ?, ?>> {
326 NamespaceMutation(final StatementContextBase<?, ?, ?> ctx, final Class<N> namespace) {
331 private final class AddedToNamespace<C extends StmtContext<?, ?, ?>> extends AbstractPrerequisite<C>
332 implements OnNamespaceItemAdded, OnPhaseFinished {
333 private final ModelProcessingPhase phase;
335 AddedToNamespace(final ModelProcessingPhase phase) {
336 this.phase = requireNonNull(phase);
340 public void namespaceItemAdded(final StatementContextBase<?, ?, ?> context, final Class<?> namespace,
341 final Object key, final Object value) {
342 ((StatementContextBase<?, ?, ?>) value).addPhaseCompletedListener(phase, this);
345 @SuppressWarnings("unchecked")
347 public boolean phaseFinished(final StatementContextBase<?, ?, ?> context,
348 final ModelProcessingPhase finishedPhase) {
349 return resolvePrereq((C) context) || tryApply();
353 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
354 return super.addToStringAttributes(toStringHelper).add("phase", phase);
358 private final class PhaseModificationInNamespace<C extends Mutable<?, ?, ?>> extends AbstractPrerequisite<C>
359 implements OnNamespaceItemAdded, ContextMutation {
360 private final ModelProcessingPhase modPhase;
362 PhaseModificationInNamespace(final ModelProcessingPhase phase) {
363 checkArgument(phase != null, "Model processing phase must not be null");
364 this.modPhase = phase;
367 @SuppressWarnings("unchecked")
369 public void namespaceItemAdded(final StatementContextBase<?, ?, ?> context, final Class<?> namespace,
370 final Object key, final Object value) {
371 StatementContextBase<?, ?, ?> targetCtx = contextImpl(value);
372 targetCtx.addMutation(modPhase, this);
373 resolvePrereq((C) targetCtx);
377 public boolean isFinished() {
383 * This similar to {@link PhaseModificationInNamespace}, but allows recursive descent until it finds the real
384 * target. The mechanics is driven as a sequence of prerequisites along a path: first we hook onto namespace to
385 * give us the first step. When it does, we hook onto the first item to provide us the second step and so on.
387 private final class PhaseModificationInNamespacePath<C extends Mutable<?, ?, ?>, K,
388 N extends IdentifierNamespace<K, ? extends StmtContext<?, ?, ?>>> extends AbstractPrerequisite<C>
389 implements OnNamespaceItemAdded, ContextMutation {
390 private final ModelProcessingPhase modPhase;
391 private final Iterable<K> keys;
392 private final Iterator<K> it;
394 PhaseModificationInNamespacePath(final ModelProcessingPhase phase, final Iterable<K> keys) {
395 this.modPhase = requireNonNull(phase);
396 this.keys = requireNonNull(keys);
397 it = keys.iterator();
401 public boolean isFinished() {
406 public void namespaceItemAdded(final StatementContextBase<?, ?, ?> context, final Class<?> namespace,
407 final Object key, final Object value) {
408 LOG.debug("Action for {} got key {}", keys, key);
410 final StatementContextBase<?, ?, ?> target = contextImpl(value);
411 if (!target.isSupportedByFeatures()) {
412 LOG.debug("Key {} in {} is not supported", key, keys);
414 action.prerequisiteUnavailable(this);
418 // Hook onto target: we either have a modification of the target itself or one of its children.
419 target.addMutation(modPhase, this);
420 // We have completed the context -> target step, hence we are no longer directly blocking context from
421 // making forward progress.
422 context.removeMutation(modPhase, this);
425 // Last step: we are done
426 if (resolvePrereq((C) value)) {
432 // Make sure target's storage notifies us when the next step becomes available.
433 hookOnto(target, namespace, it.next());
437 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
438 return super.addToStringAttributes(toStringHelper).add("phase", modPhase).add("keys", keys);
441 void hookOnto(final StmtContext<?, ?, ?> context, final Class<?> namespace) {
442 checkArgument(it.hasNext(), "Namespace %s keys may not be empty", namespace);
443 hookOnto(contextImpl(context), namespace, it.next());
446 @SuppressWarnings("unchecked")
447 private void hookOnto(final StatementContextBase<?, ?, ?> context, final Class<?> namespace, final K key) {
448 context.onNamespaceItemAddedAction((Class) namespace, requireNonNull(key), this);