blob: f1f14823fe0572059b97a392cee69696d75a7a61 [file] [log] [blame]
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +02001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.gnoi.ctl;
18
19import gnoi.system.SystemGrpc;
20import gnoi.system.SystemOuterClass.TimeRequest;
21import gnoi.system.SystemOuterClass.TimeResponse;
22import gnoi.system.SystemOuterClass.RebootRequest;
23import gnoi.system.SystemOuterClass.RebootResponse;
24import io.grpc.ManagedChannel;
25import io.grpc.stub.StreamObserver;
26import org.onosproject.gnoi.api.GnoiClient;
27import org.onosproject.gnoi.api.GnoiClientKey;
28import org.onosproject.grpc.ctl.AbstractGrpcClient;
29import java.util.concurrent.CompletableFuture;
30import java.util.concurrent.TimeUnit;
31import java.util.function.Consumer;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * Implementation of gNOI client.
37 */
38public class GnoiClientImpl extends AbstractGrpcClient implements GnoiClient {
39
40 private static final int RPC_TIMEOUT_SECONDS = 10;
41 private static final Logger log = LoggerFactory.getLogger(GnoiClientImpl.class);
42
43 GnoiClientImpl(GnoiClientKey clientKey, ManagedChannel managedChannel, GnoiControllerImpl controller) {
44 super(clientKey, managedChannel, false, controller);
45 }
46
47 @Override
48 public CompletableFuture<Boolean> probeService() {
49 return this.time().handle((response, t) -> {
50 if (t == null) {
51 log.debug("gNOI probeService succeed");
52 return true;
53 } else {
54 log.debug("gNOI probeService failed", t);
55 return false;
56 }
57 });
58 }
59
60 @Override
61 public CompletableFuture<TimeResponse> time() {
62 // The TimeRequest message is empty one so just form it
63 final TimeRequest requestMsg = TimeRequest.getDefaultInstance();
64 final CompletableFuture<TimeResponse> future = new CompletableFuture<>();
65
66 final StreamObserver<TimeResponse> observer =
67 new StreamObserver<TimeResponse>() {
68 @Override
69 public void onNext(TimeResponse value) {
70 future.complete(value);
71 }
72 @Override
73 public void onError(Throwable t) {
74 handleRpcError(t, "gNOI time request");
75 future.completeExceptionally(t);
76 }
77 @Override
78 public void onCompleted() {
79 // ignore
80 }
81 };
82
83 execRpc(s -> s.time(requestMsg, observer));
84 return future;
85 }
86
87 @Override
88 public CompletableFuture<RebootResponse> reboot(RebootRequest request) {
89 final CompletableFuture<RebootResponse> future = new CompletableFuture<>();
90
91 final StreamObserver<RebootResponse> observer =
92 new StreamObserver<RebootResponse>() {
93 @Override
94 public void onNext(RebootResponse value) {
95 future.complete(value);
96 }
97 @Override
98 public void onError(Throwable t) {
99 handleRpcError(t, "gNOI reboot request");
100 future.completeExceptionally(t);
101 }
102 @Override
103 public void onCompleted() {
104 // ignore
105 }
106 };
107
108 execRpc(s -> s.reboot(request, observer));
109 return future;
110 }
111
112 /**
113 * Forces execution of an RPC in a cancellable context with a timeout.
114 *
115 * @param stubConsumer SystemStub stub consumer
116 */
117 private void execRpc(Consumer<SystemGrpc.SystemStub> stubConsumer) {
118 if (log.isTraceEnabled()) {
119 log.trace("Executing RPC with timeout {} seconds (context deadline {})...",
120 RPC_TIMEOUT_SECONDS, context().getDeadline());
121 }
122 runInCancellableContext(() -> stubConsumer.accept(
123 SystemGrpc.newStub(channel)
124 .withDeadlineAfter(RPC_TIMEOUT_SECONDS, TimeUnit.SECONDS)));
125 }
126
127 /**
128 * Forces execution of an RPC in a cancellable context with no timeout.
129 *
130 * @param stubConsumer SystemStub stub consumer
131 */
132 void execRpcNoTimeout(Consumer<SystemGrpc.SystemStub> stubConsumer) {
133 if (log.isTraceEnabled()) {
134 log.trace("Executing RPC with no timeout (context deadline {})...",
135 context().getDeadline());
136 }
137 runInCancellableContext(() -> stubConsumer.accept(
138 SystemGrpc.newStub(channel)));
139 }
140
141}