Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / ProxyYangTextSourceProvider.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.singleton.impl;
10
11 import akka.actor.ActorContext;
12 import akka.actor.ActorRef;
13 import akka.dispatch.Futures;
14 import akka.dispatch.OnComplete;
15 import akka.pattern.Patterns;
16 import com.google.common.collect.Sets;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
20 import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy;
21 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
22 import org.opendaylight.netconf.topology.singleton.messages.YangTextSchemaSourceRequest;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import scala.concurrent.Future;
25 import scala.concurrent.impl.Promise;
26
27 public class ProxyYangTextSourceProvider implements RemoteYangTextSourceProvider {
28
29     private final ActorRef masterRef;
30     private final ActorContext actorContext;
31
32     public ProxyYangTextSourceProvider(final ActorRef masterRef, final ActorContext actorContext) {
33         this.masterRef = masterRef;
34         this.actorContext = actorContext;
35     }
36
37     @Override
38     public Future<Set<SourceIdentifier>> getProvidedSources() {
39         // NOOP
40         return Futures.successful(Sets.newHashSet());
41     }
42
43     @Override
44     public Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(
45             @Nonnull final SourceIdentifier sourceIdentifier) {
46
47         final Future<Object> scalaFuture = Patterns.ask(masterRef,
48                 new YangTextSchemaSourceRequest(sourceIdentifier), NetconfTopologyUtils.TIMEOUT);
49
50         final Promise.DefaultPromise<YangTextSchemaSourceSerializationProxy> promise = new Promise.DefaultPromise<>();
51
52         scalaFuture.onComplete(new OnComplete<Object>() {
53             @Override
54             public void onComplete(final Throwable failure, final Object success) throws Throwable {
55                 if (failure != null) {
56                     promise.failure(failure);
57                     return;
58                 }
59                 if (success instanceof Throwable) {
60                     promise.failure((Throwable) success);
61                     return;
62                 }
63                 promise.success((YangTextSchemaSourceSerializationProxy) success);
64             }
65         }, actorContext.dispatcher());
66
67         return promise.future();
68
69     }
70 }