Improve segmented journal actor metrics
[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.api.source.SourceIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
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
22 /**
23  * Provides schema sources from {@link RemoteYangTextSourceProvider}.
24  */
25 @Beta
26 public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSource> {
27     private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);
28
29     private final RemoteYangTextSourceProvider remoteRepo;
30     private final ExecutionContext executionContext;
31
32     public RemoteSchemaProvider(final RemoteYangTextSourceProvider remoteRepo,
33             final ExecutionContext executionContext) {
34         this.remoteRepo = remoteRepo;
35         this.executionContext = executionContext;
36     }
37
38     @Override
39     public ListenableFuture<YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
40         LOG.trace("Getting yang schema source for {}", sourceIdentifier.name().getLocalName());
41
42         final var res = SettableFuture.<YangTextSource>create();
43         remoteRepo.getYangTextSchemaSource(sourceIdentifier).onComplete(new OnComplete<>() {
44             @Override
45             public void onComplete(final Throwable failure, final YangTextSchemaSourceSerializationProxy success) {
46                 if (success != null) {
47                     res.set(success.getRepresentation());
48                 }
49                 if (failure != null) {
50                     res.setException(failure);
51                 }
52             }
53         }, executionContext);
54
55         return res;
56     }
57 }