c0d1aa4545ebe64443e8b990a22c5dfe4a59f4a9
[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 package org.opendaylight.netconf.topology.singleton.impl;
9
10 import akka.actor.ActorRef;
11 import akka.dispatch.Futures;
12 import akka.dispatch.OnComplete;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import java.util.Collections;
16 import java.util.Set;
17 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
18 import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy;
19 import org.opendaylight.netconf.topology.singleton.messages.YangTextSchemaSourceRequest;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import scala.concurrent.ExecutionContext;
22 import scala.concurrent.Future;
23 import scala.concurrent.impl.Promise;
24
25 public class ProxyYangTextSourceProvider implements RemoteYangTextSourceProvider {
26
27     private final ActorRef masterRef;
28     private final ExecutionContext executionContext;
29     private final Timeout actorResponseWaitTime;
30
31     public ProxyYangTextSourceProvider(final ActorRef masterRef, final ExecutionContext executionContext,
32                                        final Timeout actorResponseWaitTime) {
33         this.masterRef = masterRef;
34         this.executionContext = executionContext;
35         this.actorResponseWaitTime = actorResponseWaitTime;
36     }
37
38     @Override
39     public Future<Set<SourceIdentifier>> getProvidedSources() {
40         // NOOP
41         return Futures.successful(Collections.emptySet());
42     }
43
44     @Override
45     public Future<YangTextSchemaSourceSerializationProxy> getYangTextSchemaSource(
46             final SourceIdentifier sourceIdentifier) {
47
48         final Future<Object> scalaFuture = Patterns.ask(masterRef,
49                 new YangTextSchemaSourceRequest(sourceIdentifier), actorResponseWaitTime);
50
51         final Promise.DefaultPromise<YangTextSchemaSourceSerializationProxy> promise = new Promise.DefaultPromise<>();
52
53         scalaFuture.onComplete(new OnComplete<Object>() {
54             @Override
55             public void onComplete(final Throwable failure, final Object success) {
56                 if (failure != null) {
57                     promise.failure(failure);
58                     return;
59                 }
60
61                 promise.success((YangTextSchemaSourceSerializationProxy) success);
62             }
63         }, executionContext);
64
65         return promise.future();
66     }
67 }