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 java.util.HashMap;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
18 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
29 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
30 implements SchemaSourceListener, SchemaSourceProvider<D> {
31 private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create(
32 "Source transformation", SchemaSourceException.class);
35 public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
36 extends AsyncFunction<S, D> {
38 CheckedFuture<D, SchemaSourceException> apply(@Nonnull final S input) throws Exception;
41 private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = new HashMap<>();
42 private final SchemaSourceRegistry consumer;
43 private final SchemaRepository provider;
44 private final AsyncFunction<S, D> function;
45 private final Class<S> srcClass;
46 private final Class<D> dstClass;
48 public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
49 final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
50 this.provider = Preconditions.checkNotNull(provider);
51 this.consumer = Preconditions.checkNotNull(consumer);
52 this.function = Preconditions.checkNotNull(function);
53 this.srcClass = Preconditions.checkNotNull(srcClass);
54 this.dstClass = Preconditions.checkNotNull(dstClass);
58 public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
59 final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
60 return Futures.makeChecked(Futures.transform(f, function), MAPPER);
64 public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
69 public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
70 for (PotentialSchemaSource<?> src : sources) {
71 final Class<?> rep = src.getRepresentation();
72 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
79 public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
80 final Class<?> rep = source.getRepresentation();
81 if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
82 unregisterSource(source);
86 private void registerSource(final PotentialSchemaSource<?> src) {
87 RefcountedRegistration reg = sources.get(src);
93 final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
94 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
96 final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
97 sources.put(src, new RefcountedRegistration(r));
100 private void unregisterSource(final PotentialSchemaSource<?> src) {
101 final RefcountedRegistration reg = sources.get(src);
102 if (reg != null && reg.decRef()) {