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