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.mdsal.binding.dom.adapter;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.concurrent.CopyOnWriteArrayList;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 class FutureSchema implements AutoCloseable {
27 private final List<FutureSchemaPredicate> postponedOperations = new CopyOnWriteArrayList<>();
28 private final SettableFuture<?> schemaPromise = SettableFuture.create();
29 private final long duration;
30 private final TimeUnit unit;
32 protected FutureSchema(final long time, final TimeUnit unit) {
37 void onRuntimeContextUpdated(final BindingRuntimeContext context) {
38 for (final FutureSchemaPredicate op : postponedOperations) {
39 op.unlockIfPossible(context);
53 for (final FutureSchemaPredicate op : postponedOperations) {
58 private static boolean isSchemaAvailable(final Class<?> clz, final BindingRuntimeContext context) {
60 if (Augmentation.class.isAssignableFrom(clz)) {
61 schema = context.getAugmentationDefinition(clz);
63 schema = context.getSchemaDefinition(clz);
65 return schema != null;
68 boolean waitForSchema(final URI namespace, final Date revision) {
69 final FutureSchemaPredicate postponedOp = new FutureSchemaPredicate() {
72 public boolean apply(final BindingRuntimeContext input) {
73 return input.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision) != null;
76 return postponedOp.waitForSchema();
79 boolean waitForSchema(final Collection<Class<?>> bindingClasses) {
80 final FutureSchemaPredicate postponedOp = new FutureSchemaPredicate() {
83 public boolean apply(final BindingRuntimeContext context) {
84 for (final Class<?> clz : bindingClasses) {
85 if (!isSchemaAvailable(clz, context)) {
92 return postponedOp.waitForSchema();
95 private abstract class FutureSchemaPredicate implements Predicate<BindingRuntimeContext> {
97 final boolean waitForSchema() {
99 schemaPromise.get(duration, unit);
101 } catch (final InterruptedException | ExecutionException e) {
102 throw Throwables.propagate(e);
103 } catch (final TimeoutException e) {
106 postponedOperations.remove(this);
110 final void unlockIfPossible(final BindingRuntimeContext context) {
111 if (!schemaPromise.isDone() && apply(context)) {
112 schemaPromise.set(null);
116 final void cancel() {
117 schemaPromise.cancel(true);