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
9 package org.opendaylight.controller.md.sal.binding.impl;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.SettableFuture;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedHashSet;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
24 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
27 class FutureSchema implements AutoCloseable {
29 @GuardedBy(value="postponedOperations")
30 private final Set<FutureSchemaPredicate> postponedOperations = new LinkedHashSet<>();
31 private final long duration;
32 private final TimeUnit unit;
33 private final boolean waitEnabled;
34 private volatile BindingRuntimeContext runtimeContext;
36 protected FutureSchema(final long time, final TimeUnit unit, final boolean waitEnabled) {
39 this.waitEnabled = waitEnabled;
42 BindingRuntimeContext runtimeContext() {
43 final BindingRuntimeContext localRuntimeContext = this.runtimeContext;
44 if(localRuntimeContext != null) {
45 return localRuntimeContext;
48 if(waitForSchema(Collections.emptyList())) {
49 return this.runtimeContext;
52 throw new IllegalStateException("No SchemaContext is available");
55 void onRuntimeContextUpdated(final BindingRuntimeContext context) {
56 synchronized(this.postponedOperations) {
57 this.runtimeContext = context;
58 for (final FutureSchemaPredicate op : this.postponedOperations) {
59 op.unlockIfPossible(context);
74 synchronized(this.postponedOperations) {
75 for (final FutureSchemaPredicate op : this.postponedOperations) {
81 private static boolean isSchemaAvailable(final Class<?> clz, final BindingRuntimeContext context) {
83 if (Augmentation.class.isAssignableFrom(clz)) {
84 schema = context.getAugmentationDefinition(clz);
86 schema = context.getSchemaDefinition(clz);
88 return schema != null;
91 boolean waitForSchema(final QNameModule module) {
92 return addPostponedOpAndWait(new FutureSchemaPredicate() {
94 public boolean apply(final BindingRuntimeContext input) {
95 return input.getSchemaContext().findModule(module).isPresent();
100 boolean waitForSchema(final Collection<Class<?>> bindingClasses) {
101 return addPostponedOpAndWait(new FutureSchemaPredicate() {
103 public boolean apply(final BindingRuntimeContext context) {
104 for (final Class<?> clz : bindingClasses) {
105 if (!isSchemaAvailable(clz, context)) {
114 private boolean addPostponedOpAndWait(final FutureSchemaPredicate postponedOp) {
115 if(!this.waitEnabled) {
119 final BindingRuntimeContext localRuntimeContext = this.runtimeContext;
120 synchronized(this.postponedOperations) {
121 this.postponedOperations.add(postponedOp);
123 // If the runtimeContext changed, this op may now be satisfied so check it.
124 if(localRuntimeContext != this.runtimeContext) {
125 postponedOp.unlockIfPossible(this.runtimeContext);
129 return postponedOp.waitForSchema();
132 private abstract class FutureSchemaPredicate implements Predicate<BindingRuntimeContext> {
134 final boolean waitForSchema() {
136 this.schemaPromise.get(FutureSchema.this.duration, FutureSchema.this.unit);
138 } catch (final InterruptedException | ExecutionException e) {
139 throw Throwables.propagate(e);
140 } catch (final TimeoutException e) {
143 synchronized(FutureSchema.this.postponedOperations) {
144 FutureSchema.this.postponedOperations.remove(this);
149 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
150 final void unlockIfPossible(final BindingRuntimeContext context) {
151 if (!this.schemaPromise.isDone() && apply(context)) {
152 this.schemaPromise.set(null);
156 final void cancel() {
157 this.schemaPromise.cancel(true);
160 private final SettableFuture<?> schemaPromise = SettableFuture.create();