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.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.HashMap;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
20 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
29 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
31 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
32 implements SchemaSourceListener, SchemaSourceProvider<D> {
33 private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create(
34 "Source transformation", SchemaSourceException.class);
37 public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
38 extends AsyncFunction<S, D> {
40 CheckedFuture<D, SchemaSourceException> apply(@Nonnull S input) throws Exception;
43 private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = new HashMap<>();
44 private final SchemaSourceRegistry consumer;
45 private final SchemaRepository provider;
46 private final AsyncFunction<S, D> function;
47 private final Class<S> srcClass;
48 private final Class<D> dstClass;
50 public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
51 final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
52 this.provider = requireNonNull(provider);
53 this.consumer = requireNonNull(consumer);
54 this.function = requireNonNull(function);
55 this.srcClass = requireNonNull(srcClass);
56 this.dstClass = requireNonNull(dstClass);
60 public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
61 final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
62 return Futures.makeChecked(Futures.transformAsync(f, function, MoreExecutors.directExecutor()), MAPPER);
66 public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
71 public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
72 for (PotentialSchemaSource<?> src : sources) {
73 final Class<?> rep = src.getRepresentation();
74 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
81 public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
82 final Class<?> rep = source.getRepresentation();
83 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
84 unregisterSource(source);
88 private void registerSource(final PotentialSchemaSource<?> src) {
89 RefcountedRegistration reg = sources.get(src);
95 final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
96 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
98 final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
99 sources.put(src, new RefcountedRegistration(r));
102 private void unregisterSource(final PotentialSchemaSource<?> src) {
103 final RefcountedRegistration reg = sources.get(src);
104 if (reg != null && reg.decRef()) {