2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.schema.provider.impl;
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.provider.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;
28 * Provides schema sources from {@link RemoteYangTextSourceProvider}.
31 public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchemaSource> {
33 private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);
35 private final RemoteYangTextSourceProvider remoteRepo;
36 private final ExecutionContext executionContext;
38 private static final ExceptionMapper<SchemaSourceException> MAPPER = new ExceptionMapper<SchemaSourceException>(
39 "schemaDownload", SchemaSourceException.class) {
41 protected SchemaSourceException newWithCause(final String message, final Throwable throwable) {
42 return new SchemaSourceException(message, throwable);
46 public RemoteSchemaProvider(RemoteYangTextSourceProvider remoteRepo, ExecutionContext executionContext) {
47 this.remoteRepo = remoteRepo;
48 this.executionContext = executionContext;
52 public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
53 LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());
55 Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);
57 final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
58 result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
60 public void onComplete(Throwable throwable,
61 YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
62 if (yangTextSchemaSourceSerializationProxy != null) {
63 res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
65 if (throwable != null) {
66 res.setException(throwable);
72 return Futures.makeChecked(res, MAPPER);