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