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