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 | */ |
| 16 | package protocols.gnmi.ctl.java.org.onosproject.gnmi.ctl; |
| 17 | |
| 18 | import gnmi.Gnmi.CapabilityRequest; |
| 19 | import gnmi.Gnmi.CapabilityResponse; |
| 20 | import gnmi.Gnmi.GetRequest; |
| 21 | import gnmi.Gnmi.GetResponse; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 22 | import gnmi.Gnmi.Path; |
| 23 | import gnmi.Gnmi.PathElem; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 24 | import gnmi.Gnmi.SetRequest; |
| 25 | import gnmi.Gnmi.SetResponse; |
| 26 | import gnmi.gNMIGrpc; |
| 27 | import io.grpc.ManagedChannel; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 28 | import io.grpc.Status; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 29 | import io.grpc.StatusRuntimeException; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame^] | 30 | import org.onosproject.gnmi.api.GnmiClient; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 31 | import org.onosproject.gnmi.api.GnmiClientKey; |
| 32 | import org.onosproject.grpc.ctl.AbstractGrpcClient; |
| 33 | import org.slf4j.Logger; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.concurrent.CompletableFuture; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Implementation of gNMI client. |
| 41 | */ |
| 42 | public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient { |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 43 | private static final PathElem DUMMY_PATH_ELEM = PathElem.newBuilder().setName("onos-gnmi-test").build(); |
| 44 | private static final Path DUMMY_PATH = Path.newBuilder().addElem(DUMMY_PATH_ELEM).build(); |
| 45 | private static final GetRequest DUMMY_REQUEST = GetRequest.newBuilder().addPath(DUMMY_PATH).build(); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 46 | private final Logger log = getLogger(getClass()); |
| 47 | private final gNMIGrpc.gNMIBlockingStub blockingStub; |
| 48 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame^] | 49 | GnmiClientImpl(GnmiClientKey clientKey, ManagedChannel managedChannel) { |
| 50 | super(clientKey); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 51 | this.blockingStub = gNMIGrpc.newBlockingStub(managedChannel); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public CompletableFuture<CapabilityResponse> capability() { |
| 56 | return supplyInContext(this::doCapability, "capability"); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public CompletableFuture<GetResponse> get(GetRequest request) { |
| 61 | return supplyInContext(() -> doGet(request), "get"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public CompletableFuture<SetResponse> set(SetRequest request) { |
| 66 | return supplyInContext(() -> doSet(request), "set"); |
| 67 | } |
| 68 | |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 69 | @Override |
| 70 | public CompletableFuture<Boolean> isServiceAvailable() { |
| 71 | return supplyInContext(this::doServiceAvailable, "isServiceAvailable"); |
| 72 | } |
| 73 | |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 74 | private CapabilityResponse doCapability() { |
| 75 | CapabilityRequest request = CapabilityRequest.newBuilder().build(); |
| 76 | try { |
| 77 | return blockingStub.capabilities(request); |
| 78 | } catch (StatusRuntimeException e) { |
| 79 | log.warn("Unable to get capability from {}: {}", deviceId, e.getMessage()); |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private GetResponse doGet(GetRequest request) { |
| 85 | try { |
| 86 | return blockingStub.get(request); |
| 87 | } catch (StatusRuntimeException e) { |
| 88 | log.warn("Unable to get data from {}: {}", deviceId, e.getMessage()); |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private SetResponse doSet(SetRequest request) { |
| 94 | try { |
| 95 | return blockingStub.set(request); |
| 96 | } catch (StatusRuntimeException e) { |
| 97 | log.warn("Unable to set data to {}: {}", deviceId, e.getMessage()); |
| 98 | return null; |
| 99 | } |
| 100 | } |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 101 | |
| 102 | private boolean doServiceAvailable() { |
| 103 | try { |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame^] | 104 | return blockingStub.get(DUMMY_REQUEST) != null; |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 105 | } catch (StatusRuntimeException e) { |
| 106 | // This gRPC call should throw INVALID_ARGUMENT status exception |
| 107 | // since "/onos-gnmi-test" path does not exists in any config model |
| 108 | // For other status code such as UNIMPLEMENT, means the gNMI |
| 109 | // service is not available on the device. |
| 110 | return e.getStatus().getCode().equals(Status.Code.INVALID_ARGUMENT); |
| 111 | } |
| 112 | } |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 113 | } |