4aef04b2dfa3744b283a577a2ccd50da85852fb0
[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  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.util;
9
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;
16 import java.util.Map;
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;
29
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);
34
35     @FunctionalInterface
36     public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
37             extends AsyncFunction<S, D> {
38         @Override
39         CheckedFuture<D, SchemaSourceException> apply(@Nonnull S input) throws Exception;
40     }
41
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;
48
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);
56     }
57
58     @Override
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);
62     }
63
64     @Override
65     public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
66         // Not interesting
67     }
68
69     @Override
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) {
74                 registerSource(src);
75             }
76         }
77     }
78
79     @Override
80     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
81         final Class<?> rep = source.getRepresentation();
82         if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
83             unregisterSource(source);
84         }
85     }
86
87     private void registerSource(final PotentialSchemaSource<?> src) {
88         RefcountedRegistration reg = sources.get(src);
89         if (reg != null) {
90             reg.incRef();
91             return;
92         }
93
94         final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
95                 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
96
97         final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
98         sources.put(src, new RefcountedRegistration(r));
99     }
100
101     private void unregisterSource(final PotentialSchemaSource<?> src) {
102         final RefcountedRegistration reg = sources.get(src);
103         if (reg != null && reg.decRef()) {
104             sources.remove(src);
105         }
106     }
107 }