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.util;
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;
21 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
27 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
28 implements SchemaSourceListener, SchemaSourceProvider<D> {
31 public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
32 extends AsyncFunction<S, D> {
34 ListenableFuture<D> apply(S input) throws Exception;
37 private final Map<PotentialSchemaSource<?>, RefcountedRegistration> availableSources = new HashMap<>();
38 private final SchemaSourceRegistry consumer;
39 private final SchemaRepository provider;
40 private final AsyncFunction<S, D> function;
41 private final Class<S> srcClass;
42 private final Class<D> dstClass;
44 public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
45 final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
46 this.provider = requireNonNull(provider);
47 this.consumer = requireNonNull(consumer);
48 this.function = requireNonNull(function);
49 this.srcClass = requireNonNull(srcClass);
50 this.dstClass = requireNonNull(dstClass);
54 public ListenableFuture<D> getSource(final SourceIdentifier sourceIdentifier) {
55 return Futures.transformAsync(provider.getSchemaSource(sourceIdentifier, srcClass), function,
56 MoreExecutors.directExecutor());
60 public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
65 public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
66 for (PotentialSchemaSource<?> src : sources) {
67 final Class<?> rep = src.getRepresentation();
68 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
75 public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
76 final Class<?> rep = source.getRepresentation();
77 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
78 unregisterSource(source);
82 private void registerSource(final PotentialSchemaSource<?> src) {
83 RefcountedRegistration reg = availableSources.get(src);
89 final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
90 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
92 final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
93 availableSources.put(src, new RefcountedRegistration(r));
96 private void unregisterSource(final PotentialSchemaSource<?> src) {
97 final RefcountedRegistration reg = availableSources.get(src);
98 if (reg != null && reg.decRef()) {
99 availableSources.remove(src);