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