Move model.repo.util classes to model.repo.spi
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / spi / SchemaSourceTransformer.java
index 13d309ed3f792893bc226dd368c4c03639163c65..30764f5741157822046237670bdfe79c8031c9aa 100644 (file)
@@ -3,52 +3,95 @@
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/eplv10.html
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.model.repo.spi;
 
-import com.google.common.util.concurrent.CheckedFuture;
+import static java.util.Objects.requireNonNull;
 
+import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
-/**
- * An schema source representation transformation service. An instance can create
- * some output schema source representation based on some input source representation.
- *
- * @param <I> Input {@link SchemaSourceRepresentation}
- * @param <O> Output {@link SchemaSourceRepresentation}
- */
-public interface SchemaSourceTransformer<I extends SchemaSourceRepresentation, O extends SchemaSourceRepresentation> {
-    /**
-     * Return the {@link SchemaSourceRepresentation} which this transformer
-     * accepts on its input.
-     *
-     * @return The input source representation type.
-     */
-    Class<I> getInputRepresentation();
-
-    /**
-     * Return the {@link SchemeSourceRepresentation} which this transformer
-     * produces on its output.
-     *
-     * @return The output source representation type.
-     */
-    Class<O> getOutputRepresentation();
-
-    /**
-     * Transform a schema source representation from its input form to
-     * the transformers output form.
-     *
-     * @param source Schema source in its source representation
-     * @return A future which produces the output schema source representation.
-     */
-    CheckedFuture<O, SchemaSourceTransformationException> transformSchemaSource(I source);
-
-    /**
-     * Return the relative cost of performing the transformation. When in doubt,
-     * return 1.
-     *
-     * @return Relative cost.
-     */
-    int getCost();
+public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
+        implements SchemaSourceListener, SchemaSourceProvider<D> {
+
+    @FunctionalInterface
+    public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
+            extends AsyncFunction<S, D> {
+        @Override
+        ListenableFuture<D> apply(S input) throws Exception;
+    }
+
+    private final Map<PotentialSchemaSource<?>, RefcountedRegistration> availableSources = new HashMap<>();
+    private final SchemaSourceRegistry consumer;
+    private final SchemaRepository provider;
+    private final AsyncFunction<S, D> function;
+    private final Class<S> srcClass;
+    private final Class<D> dstClass;
+
+    public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
+            final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
+        this.provider = requireNonNull(provider);
+        this.consumer = requireNonNull(consumer);
+        this.function = requireNonNull(function);
+        this.srcClass = requireNonNull(srcClass);
+        this.dstClass = requireNonNull(dstClass);
+    }
+
+    @Override
+    public final ListenableFuture<D> getSource(final SourceIdentifier sourceIdentifier) {
+        return Futures.transformAsync(provider.getSchemaSource(sourceIdentifier, srcClass), function,
+            MoreExecutors.directExecutor());
+    }
+
+    @Override
+    public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
+        // Not interesting
+    }
+
+    @Override
+    public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
+        for (PotentialSchemaSource<?> src : sources) {
+            final Class<?> rep = src.getRepresentation();
+            if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
+                registerSource(src);
+            }
+        }
+    }
+
+    @Override
+    public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
+        final Class<?> rep = source.getRepresentation();
+        if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
+            unregisterSource(source);
+        }
+    }
+
+    private void registerSource(final PotentialSchemaSource<?> src) {
+        RefcountedRegistration reg = availableSources.get(src);
+        if (reg != null) {
+            reg.incRef();
+            return;
+        }
+
+        final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
+                src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
+
+        final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
+        availableSources.put(src, new RefcountedRegistration(r));
+    }
+
+    private void unregisterSource(final PotentialSchemaSource<?> src) {
+        final RefcountedRegistration reg = availableSources.get(src);
+        if (reg != null && reg.decRef()) {
+            availableSources.remove(src);
+        }
+    }
 }