Fix license header violations 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
15 import java.util.HashMap;
16 import java.util.Map;
17
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> implements SchemaSourceListener, SchemaSourceProvider<D> {
31     private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create("Source transformation", SchemaSourceException.class);
32
33     public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation> extends AsyncFunction<S, D> {
34         @Override
35         CheckedFuture<D, SchemaSourceException> apply(final S input) throws Exception;
36     }
37
38     private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = new HashMap<>();
39     private final SchemaSourceRegistry consumer;
40     private final SchemaRepository provider;
41     private final AsyncFunction<S, D> function;
42     private final Class<S> srcClass;
43     private final Class<D> dstClass;
44
45     public SchemaSourceTransformer(final SchemaRepository provider, final Class<S> srcClass,
46             final SchemaSourceRegistry consumer, final Class<D> dstClass, final AsyncFunction<S, D> function) {
47         this.provider = Preconditions.checkNotNull(provider);
48         this.consumer = Preconditions.checkNotNull(consumer);
49         this.function = Preconditions.checkNotNull(function);
50         this.srcClass = Preconditions.checkNotNull(srcClass);
51         this.dstClass = Preconditions.checkNotNull(dstClass);
52     }
53
54     @Override
55     public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
56         final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
57         return Futures.makeChecked(Futures.transform(f, function), MAPPER);
58     }
59
60     @Override
61     public final void schemaSourceEncountered(final SchemaSourceRepresentation source) {
62         // Not interesting
63     }
64
65     @Override
66     public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
67         for (PotentialSchemaSource<?> src : sources) {
68             final Class<?> rep = src.getRepresentation();
69             if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
70                 registerSource(src);
71             }
72         }
73     }
74
75     @Override
76     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
77         final Class<?> rep = source.getRepresentation();
78         if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
79             unregisterSource(source);
80         }
81     }
82
83     private void registerSource(final PotentialSchemaSource<?> src) {
84         RefcountedRegistration reg = sources.get(src);
85         if (reg != null) {
86             reg.incRef();
87             return;
88         }
89
90         final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
91                 src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
92
93         final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
94         sources.put(src, new RefcountedRegistration(r));
95     }
96
97     private void unregisterSource(final PotentialSchemaSource<?> src) {
98         final RefcountedRegistration reg = sources.get(src);
99         if (reg != null && reg.decRef()) {
100             sources.remove(src);
101         }
102     }
103 }