2 * Copyright (c) 2014 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.model.repo.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.HashMap;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
23 implements SchemaSourceListener, SchemaSourceProvider<D> {
26 public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
27 extends AsyncFunction<S, D> {
29 ListenableFuture<D> apply(S input) throws Exception;
32 private final Map<PotentialSchemaSource<?>, RefcountedRegistration> availableSources = new HashMap<>();
33 private final SchemaSourceRegistry consumer;
34 private final SchemaRepository provider;
35 private final AsyncFunction<S, D> function;
36 private final Class<S> srcClass;
37 private final Class<D> dstClass;
39 public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
40 final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
41 this.provider = requireNonNull(provider);
42 this.consumer = requireNonNull(consumer);
43 this.function = requireNonNull(function);
44 this.srcClass = requireNonNull(srcClass);
45 this.dstClass = requireNonNull(dstClass);
49 public final ListenableFuture<D> getSource(final SourceIdentifier sourceIdentifier) {
50 return Futures.transformAsync(provider.getSchemaSource(sourceIdentifier, srcClass), function,
51 MoreExecutors.directExecutor());
55 public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
60 public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
61 for (PotentialSchemaSource<?> src : sources) {
62 final Class<?> rep = src.getRepresentation();
63 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
70 public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
71 final Class<?> rep = source.getRepresentation();
72 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
73 unregisterSource(source);
77 private void registerSource(final PotentialSchemaSource<?> src) {
78 RefcountedRegistration reg = availableSources.get(src);
84 final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
85 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
87 final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
88 availableSources.put(src, new RefcountedRegistration(r));
91 private void unregisterSource(final PotentialSchemaSource<?> src) {
92 final RefcountedRegistration reg = availableSources.get(src);
93 if (reg != null && reg.decRef()) {
94 availableSources.remove(src);