Reduce JSR305 proliferation
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteYangTextSourceProviderImpl.java
1 /*
2  * Copyright (c) 2015 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.controller.cluster.schema.provider.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.io.IOException;
18 import java.util.Set;
19 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.Future;
26 import scala.concurrent.Promise;
27
28 /**
29  *  Remote schema provider implementation backed by local schema provider.
30  */
31 @Beta
32 public class RemoteYangTextSourceProviderImpl implements RemoteYangTextSourceProvider {
33     private static final Logger LOG = LoggerFactory.getLogger(RemoteYangTextSourceProviderImpl.class);
34
35     private final SchemaRepository repository;
36     private final Set<SourceIdentifier> providedSources;
37
38     public RemoteYangTextSourceProviderImpl(final SchemaRepository repository,
39             final Set<SourceIdentifier> providedSources) {
40         this.repository = requireNonNull(repository);
41         this.providedSources = providedSources;
42     }
43
44     @Override
45     public Future<Set<SourceIdentifier>> getProvidedSources() {
46         return akka.dispatch.Futures.successful(providedSources);
47     }
48
49     @Override
50     public Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(final SourceIdentifier identifier) {
51         LOG.trace("Sending yang schema source for {}", identifier);
52
53         final Promise<YangTextSchemaSourceSerializationProxy> promise = akka.dispatch.Futures.promise();
54         ListenableFuture<YangTextSchemaSource> future =
55                 repository.getSchemaSource(identifier, YangTextSchemaSource.class);
56
57         Futures.addCallback(future, new FutureCallback<YangTextSchemaSource>() {
58             @Override
59             public void onSuccess(final YangTextSchemaSource result) {
60                 try {
61                     promise.success(new YangTextSchemaSourceSerializationProxy(result));
62                 } catch (IOException e) {
63                     LOG.warn("Unable to read schema source for {}", result.getIdentifier(), e);
64                     promise.failure(e);
65                 }
66             }
67
68             @Override
69             public void onFailure(final Throwable failure) {
70                 LOG.warn("Unable to retrieve schema source from provider", failure);
71                 promise.failure(failure);
72             }
73         }, MoreExecutors.directExecutor());
74
75         return promise.future();
76     }
77 }