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