Bump odlparent/yangtools/mdsal
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteSchemaProvider.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 akka.dispatch.OnComplete;
11 import com.google.common.annotations.Beta;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
15 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
16 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
17 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import scala.concurrent.ExecutionContext;
21 import scala.concurrent.Future;
22
23 /**
24  * Provides schema sources from {@link RemoteYangTextSourceProvider}.
25  */
26 @Beta
27 public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchemaSource> {
28     private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);
29
30     private final RemoteYangTextSourceProvider remoteRepo;
31     private final ExecutionContext executionContext;
32
33     public RemoteSchemaProvider(final RemoteYangTextSourceProvider remoteRepo,
34             final ExecutionContext executionContext) {
35         this.remoteRepo = remoteRepo;
36         this.executionContext = executionContext;
37     }
38
39     @Override
40     public ListenableFuture<YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
41         LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());
42
43         Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);
44
45         final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
46         result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
47             @Override
48             public void onComplete(final Throwable throwable,
49                     final YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
50                 if (yangTextSchemaSourceSerializationProxy != null) {
51                     res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
52                 }
53                 if (throwable != null) {
54                     res.setException(throwable);
55                 }
56             }
57         }, executionContext);
58
59         return res;
60     }
61 }