13ddf4dee36f0823417de409fd409187595d01db
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / repo / util / SchemaSourceTransformer.java
1 /*
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
6  */
7 package org.opendaylight.yangtools.yang.model.repo.util;
8
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;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
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;
28
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);
31
32     public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation> extends AsyncFunction<S, D> {
33         @Override
34         CheckedFuture<D, SchemaSourceException> apply(final S input) throws Exception;
35     }
36
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;
43
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);
51     }
52
53     @Override
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);
57     }
58
59     @Override
60     public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
61         // Not interesting
62     }
63
64     @Override
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) {
69                 registerSource(src);
70             }
71         }
72     }
73
74     @Override
75     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
76         final Class<?> rep = source.getRepresentation();
77         if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
78             unregisterSource(source);
79         }
80     }
81
82     private void registerSource(final PotentialSchemaSource<?> src) {
83         RefcountedRegistration reg = sources.get(src);
84         if (reg != null) {
85             reg.incRef();
86             return;
87         }
88
89         final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
90                 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
91
92         final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
93         sources.put(src, new RefcountedRegistration(r));
94     }
95
96     private void unregisterSource(final PotentialSchemaSource<?> src) {
97         final RefcountedRegistration reg = sources.get(src);
98         if (reg != null && reg.decRef()) {
99             sources.remove(src);
100         }
101     }
102 }