oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.gnoi.ctl; |
| 18 | |
| 19 | import gnoi.system.SystemGrpc; |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 20 | import gnoi.system.SystemOuterClass.RebootRequest; |
| 21 | import gnoi.system.SystemOuterClass.RebootResponse; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 22 | import gnoi.system.SystemOuterClass.TimeRequest; |
| 23 | import gnoi.system.SystemOuterClass.TimeResponse; |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 24 | import io.grpc.ManagedChannel; |
| 25 | import io.grpc.stub.StreamObserver; |
| 26 | import org.onosproject.gnoi.api.GnoiClient; |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 27 | import org.onosproject.grpc.ctl.AbstractGrpcClient; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | import java.util.function.Consumer; |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Implementation of gNOI client. |
| 38 | */ |
| 39 | public class GnoiClientImpl extends AbstractGrpcClient implements GnoiClient { |
| 40 | |
| 41 | private static final int RPC_TIMEOUT_SECONDS = 10; |
| 42 | private static final Logger log = LoggerFactory.getLogger(GnoiClientImpl.class); |
| 43 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 44 | GnoiClientImpl(DeviceId deviceId, ManagedChannel managedChannel, GnoiControllerImpl controller) { |
| 45 | super(deviceId, managedChannel, false, controller); |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public CompletableFuture<Boolean> probeService() { |
| 50 | return this.time().handle((response, t) -> { |
| 51 | if (t == null) { |
| 52 | log.debug("gNOI probeService succeed"); |
| 53 | return true; |
| 54 | } else { |
| 55 | log.debug("gNOI probeService failed", t); |
| 56 | return false; |
| 57 | } |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public CompletableFuture<TimeResponse> time() { |
| 63 | // The TimeRequest message is empty one so just form it |
| 64 | final TimeRequest requestMsg = TimeRequest.getDefaultInstance(); |
| 65 | final CompletableFuture<TimeResponse> future = new CompletableFuture<>(); |
| 66 | |
| 67 | final StreamObserver<TimeResponse> observer = |
| 68 | new StreamObserver<TimeResponse>() { |
| 69 | @Override |
| 70 | public void onNext(TimeResponse value) { |
| 71 | future.complete(value); |
| 72 | } |
| 73 | @Override |
| 74 | public void onError(Throwable t) { |
| 75 | handleRpcError(t, "gNOI time request"); |
| 76 | future.completeExceptionally(t); |
| 77 | } |
| 78 | @Override |
| 79 | public void onCompleted() { |
| 80 | // ignore |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | execRpc(s -> s.time(requestMsg, observer)); |
| 85 | return future; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public CompletableFuture<RebootResponse> reboot(RebootRequest request) { |
| 90 | final CompletableFuture<RebootResponse> future = new CompletableFuture<>(); |
| 91 | |
| 92 | final StreamObserver<RebootResponse> observer = |
| 93 | new StreamObserver<RebootResponse>() { |
| 94 | @Override |
| 95 | public void onNext(RebootResponse value) { |
| 96 | future.complete(value); |
| 97 | } |
| 98 | @Override |
| 99 | public void onError(Throwable t) { |
| 100 | handleRpcError(t, "gNOI reboot request"); |
| 101 | future.completeExceptionally(t); |
| 102 | } |
| 103 | @Override |
| 104 | public void onCompleted() { |
| 105 | // ignore |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | execRpc(s -> s.reboot(request, observer)); |
| 110 | return future; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Forces execution of an RPC in a cancellable context with a timeout. |
| 115 | * |
| 116 | * @param stubConsumer SystemStub stub consumer |
| 117 | */ |
| 118 | private void execRpc(Consumer<SystemGrpc.SystemStub> stubConsumer) { |
| 119 | if (log.isTraceEnabled()) { |
| 120 | log.trace("Executing RPC with timeout {} seconds (context deadline {})...", |
| 121 | RPC_TIMEOUT_SECONDS, context().getDeadline()); |
| 122 | } |
| 123 | runInCancellableContext(() -> stubConsumer.accept( |
| 124 | SystemGrpc.newStub(channel) |
| 125 | .withDeadlineAfter(RPC_TIMEOUT_SECONDS, TimeUnit.SECONDS))); |
| 126 | } |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 127 | } |