d4f0d79712bcdcd3018ef4b7aa607f961261da29
[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 akka.util.Timeout;
17 import com.google.common.collect.Sets;
18 import java.util.Set;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
21 import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy;
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     private final Timeout actorResponseWaitTime;
32
33     public ProxyYangTextSourceProvider(final ActorRef masterRef, final ActorContext actorContext,
34                                        final Timeout actorResponseWaitTime) {
35         this.masterRef = masterRef;
36         this.actorContext = actorContext;
37         this.actorResponseWaitTime = actorResponseWaitTime;
38     }
39
40     @Override
41     public Future<Set<SourceIdentifier>> getProvidedSources() {
42         // NOOP
43         return Futures.successful(Sets.newHashSet());
44     }
45
46     @Override
47     public Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(
48             @Nonnull final SourceIdentifier sourceIdentifier) {
49
50         final Future<Object> scalaFuture = Patterns.ask(masterRef,
51                 new YangTextSchemaSourceRequest(sourceIdentifier), actorResponseWaitTime);
52
53         final Promise.DefaultPromise<YangTextSchemaSourceSerializationProxy> promise = new Promise.DefaultPromise<>();
54
55         scalaFuture.onComplete(new OnComplete<Object>() {
56             @Override
57             public void onComplete(final Throwable failure, final Object success) throws Throwable {
58                 if (failure != null) {
59                     promise.failure(failure);
60                     return;
61                 }
62                 if (success instanceof Throwable) {
63                     promise.failure((Throwable) success);
64                     return;
65                 }
66                 promise.success((YangTextSchemaSourceSerializationProxy) success);
67             }
68         }, actorContext.dispatcher());
69
70         return promise.future();
71
72     }
73 }