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