Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Carmelo Cascone | 3370c96 | 2019-02-07 18:24:19 -0800 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.gnmi.ctl; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 18 | |
| 19 | import gnmi.Gnmi.CapabilityRequest; |
| 20 | import gnmi.Gnmi.CapabilityResponse; |
| 21 | import gnmi.Gnmi.GetRequest; |
| 22 | import gnmi.Gnmi.GetResponse; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 23 | import gnmi.Gnmi.Path; |
| 24 | import gnmi.Gnmi.PathElem; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 25 | import gnmi.Gnmi.SetRequest; |
| 26 | import gnmi.Gnmi.SetResponse; |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 27 | import gnmi.Gnmi.SubscribeRequest; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 28 | import gnmi.gNMIGrpc; |
| 29 | import io.grpc.ManagedChannel; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 30 | import io.grpc.Status; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 31 | import io.grpc.stub.StreamObserver; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 32 | import org.onosproject.gnmi.api.GnmiClient; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 33 | import org.onosproject.gnmi.api.GnmiClientKey; |
| 34 | import org.onosproject.grpc.ctl.AbstractGrpcClient; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 37 | import java.util.concurrent.TimeUnit; |
| 38 | import java.util.function.Consumer; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Implementation of gNMI client. |
| 42 | */ |
| 43 | public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 44 | |
| 45 | private static final int RPC_TIMEOUT_SECONDS = 10; |
| 46 | |
| 47 | private static final GetRequest PING_REQUEST = GetRequest.newBuilder().addPath( |
| 48 | Path.newBuilder().addElem( |
| 49 | PathElem.newBuilder().setName("onos-gnmi-ping").build() |
| 50 | ).build()).build(); |
| 51 | |
| 52 | private GnmiSubscriptionManager subscribeManager; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 53 | |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 54 | GnmiClientImpl(GnmiClientKey clientKey, ManagedChannel managedChannel, GnmiControllerImpl controller) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 55 | super(clientKey, managedChannel, false, controller); |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 56 | this.subscribeManager = |
| 57 | new GnmiSubscriptionManager(this, deviceId, controller); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 61 | public CompletableFuture<CapabilityResponse> capabilities() { |
| 62 | final CompletableFuture<CapabilityResponse> future = new CompletableFuture<>(); |
| 63 | execRpc(s -> s.capabilities( |
| 64 | CapabilityRequest.getDefaultInstance(), |
| 65 | unaryObserver(future, CapabilityResponse.getDefaultInstance(), |
| 66 | "capabilities request")) |
| 67 | ); |
| 68 | return future; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<GetResponse> get(GetRequest request) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 73 | final CompletableFuture<GetResponse> future = new CompletableFuture<>(); |
| 74 | execRpc(s -> s.get(request, unaryObserver( |
| 75 | future, GetResponse.getDefaultInstance(), "GET")) |
| 76 | ); |
| 77 | return future; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public CompletableFuture<SetResponse> set(SetRequest request) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 82 | final CompletableFuture<SetResponse> future = new CompletableFuture<>(); |
| 83 | execRpc(s -> s.set(request, unaryObserver( |
| 84 | future, SetResponse.getDefaultInstance(), "SET")) |
| 85 | ); |
| 86 | return future; |
| 87 | } |
| 88 | |
| 89 | private <RES> StreamObserver<RES> unaryObserver( |
| 90 | final CompletableFuture<RES> future, |
| 91 | final RES defaultResponse, |
| 92 | final String opDescription) { |
| 93 | return new StreamObserver<RES>() { |
| 94 | @Override |
| 95 | public void onNext(RES value) { |
| 96 | future.complete(value); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onError(Throwable t) { |
| 101 | handleRpcError(t, opDescription); |
| 102 | future.complete(defaultResponse); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onCompleted() { |
| 107 | // Ignore. Unary call. |
| 108 | } |
| 109 | }; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 112 | @Override |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 113 | public void subscribe(SubscribeRequest request) { |
| 114 | subscribeManager.subscribe(request); |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 118 | public void unsubscribe() { |
| 119 | subscribeManager.unsubscribe(); |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 123 | public CompletableFuture<Boolean> probeService() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 124 | final CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 125 | final StreamObserver<GetResponse> responseObserver = new StreamObserver<GetResponse>() { |
| 126 | @Override |
| 127 | public void onNext(GetResponse value) { |
| 128 | future.complete(true); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onError(Throwable t) { |
| 133 | // This gRPC call should throw INVALID_ARGUMENT status exception |
| 134 | // since "/onos-gnmi-ping" path does not exists in any config |
| 135 | // model For other status code such as UNIMPLEMENT, means the |
| 136 | // gNMI service is not available on the device. |
| 137 | future.complete(Status.fromThrowable(t).getCode() |
| 138 | == Status.Code.INVALID_ARGUMENT); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onCompleted() { |
| 143 | // Ignore. Unary call. |
| 144 | } |
| 145 | }; |
| 146 | execRpc(s -> s.get(PING_REQUEST, responseObserver)); |
| 147 | return future; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 150 | @Override |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 151 | public void shutdown() { |
| 152 | subscribeManager.shutdown(); |
| 153 | super.shutdown(); |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 156 | /** |
| 157 | * Forces execution of an RPC in a cancellable context with a timeout. |
| 158 | * |
| 159 | * @param stubConsumer P4Runtime stub consumer |
| 160 | */ |
| 161 | private void execRpc(Consumer<gNMIGrpc.gNMIStub> stubConsumer) { |
| 162 | if (log.isTraceEnabled()) { |
| 163 | log.trace("Executing RPC with timeout {} seconds (context deadline {})...", |
| 164 | RPC_TIMEOUT_SECONDS, context().getDeadline()); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 165 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 166 | runInCancellableContext(() -> stubConsumer.accept( |
| 167 | gNMIGrpc.newStub(channel) |
| 168 | .withDeadlineAfter(RPC_TIMEOUT_SECONDS, TimeUnit.SECONDS))); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 171 | /** |
| 172 | * Forces execution of an RPC in a cancellable context with no timeout. |
| 173 | * |
| 174 | * @param stubConsumer P4Runtime stub consumer |
| 175 | */ |
| 176 | void execRpcNoTimeout(Consumer<gNMIGrpc.gNMIStub> stubConsumer) { |
| 177 | if (log.isTraceEnabled()) { |
| 178 | log.trace("Executing RPC with no timeout (context deadline {})...", |
| 179 | context().getDeadline()); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 180 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 181 | runInCancellableContext(() -> stubConsumer.accept( |
| 182 | gNMIGrpc.newStub(channel))); |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 183 | } |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 184 | } |