2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.model.repo.util;
9 import com.google.common.base.Preconditions;
10 import com.google.common.util.concurrent.AsyncFunction;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
14 import java.util.HashMap;
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> implements SchemaSourceListener, SchemaSourceProvider<D> {
30 private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create("Source transformation", SchemaSourceException.class);
32 public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation> extends AsyncFunction<S, D> {
34 CheckedFuture<D, SchemaSourceException> apply(final S input) throws Exception;
37 private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = 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 = Preconditions.checkNotNull(provider);
47 this.consumer = Preconditions.checkNotNull(consumer);
48 this.function = Preconditions.checkNotNull(function);
49 this.srcClass = Preconditions.checkNotNull(srcClass);
50 this.dstClass = Preconditions.checkNotNull(dstClass);
54 public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
55 final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
56 return Futures.makeChecked(Futures.transform(f, function), MAPPER);
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 = sources.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 sources.put(src, new RefcountedRegistration(r));
96 private void unregisterSource(final PotentialSchemaSource<?> src) {
97 final RefcountedRegistration reg = sources.get(src);
98 if (reg != null && reg.decRef()) {