/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.controller.cluster.schema.provider.impl;

import akka.dispatch.OnComplete;
import com.google.common.annotations.Beta;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;

/**
 * Provides schema sources from {@link RemoteYangTextSourceProvider}.
 */
@Beta
public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchemaSource> {

    private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);

    private final RemoteYangTextSourceProvider remoteRepo;
    private final ExecutionContext executionContext;

    private static final ExceptionMapper<SchemaSourceException> MAPPER = new ExceptionMapper<SchemaSourceException>(
            "schemaDownload", SchemaSourceException.class) {
        @Override
        protected SchemaSourceException newWithCause(final String message, final Throwable throwable) {
            return new SchemaSourceException(message, throwable);
        }
    };

    public RemoteSchemaProvider(RemoteYangTextSourceProvider remoteRepo, ExecutionContext executionContext) {
        this.remoteRepo = remoteRepo;
        this.executionContext = executionContext;
    }

    @Override
    public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
        LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());

        Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);

        final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
        result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
            @Override
            public void onComplete(Throwable throwable,
                    YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
                if (yangTextSchemaSourceSerializationProxy != null) {
                    res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
                }
                if (throwable != null) {
                    res.setException(throwable);
                }
            }

        }, executionContext);

        return Futures.makeChecked(res, MAPPER);
    }
}

@@ -489,71 +489,24 @@
 ent.
-CheckedFuture;%0Aimport com.google.common.util.concurrent.
+Listenable
 Future
-s
 ;%0Aim
@@ -653,152 +653,8 @@
 er;%0A
-import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;%0Aimport org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;%0A
 impo
@@ -1179,17 +1179,16 @@
 urce%3E %7B%0A
-%0A
     priv
@@ -1393,392 +1393,8 @@
    p
-rivate static final ExceptionMapper%3CSchemaSourceException%3E MAPPER = new ExceptionMapper%3CSchemaSourceException%3E(%0A            %22schemaDownload%22, SchemaSourceException.class) %7B%0A        @Override%0A        protected SchemaSourceException newWithCause(final String message, final Throwable throwable) %7B%0A            return new SchemaSourceException(message, throwable);%0A        %7D%0A    %7D;%0A%0A    p
 ubli
@@ -1416,16 +1416,22 @@
 rovider(
+final 
 RemoteYa
@@ -1462,16 +1462,34 @@
 oteRepo,
+%0A            final
  Executi
@@ -1638,23 +1638,26 @@
  public 
-Checked
+Listenable
 Future%3CY
@@ -1679,31 +1679,8 @@
 urce
-, SchemaSourceException
 %3E ge
@@ -1687,16 +1687,22 @@
 tSource(
+final 
 SourceId
@@ -2158,16 +2158,22 @@
 omplete(
+final 
 Throwabl
@@ -2205,16 +2205,22 @@
         
+final 
 YangText
@@ -2587,25 +2587,24 @@
           %7D%0A
-%0A
         %7D, e
@@ -2641,40 +2641,11 @@
 urn 
-Futures.makeChecked(res, MAPPER)
+res
 ;%0A  

Remove CheckedFuture from RemoteSchemaProvider