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