blob: 22b226b185404d3660c002ae1dae80b88e3c5872 [file] [log] [blame]
Yi Tseng890dc3f2018-11-01 13:23:11 -07001/*
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 */
16package protocols.gnmi.ctl.java.org.onosproject.gnmi.ctl;
17
18import gnmi.Gnmi.CapabilityRequest;
19import gnmi.Gnmi.CapabilityResponse;
20import gnmi.Gnmi.GetRequest;
21import gnmi.Gnmi.GetResponse;
Yi Tseng5f7fef52018-11-05 11:30:47 -080022import gnmi.Gnmi.Path;
23import gnmi.Gnmi.PathElem;
Yi Tseng890dc3f2018-11-01 13:23:11 -070024import gnmi.Gnmi.SetRequest;
25import gnmi.Gnmi.SetResponse;
26import gnmi.gNMIGrpc;
27import io.grpc.ManagedChannel;
Yi Tseng5f7fef52018-11-05 11:30:47 -080028import io.grpc.Status;
Yi Tseng890dc3f2018-11-01 13:23:11 -070029import io.grpc.StatusRuntimeException;
Yi Tsengd7716482018-10-31 15:34:30 -070030import org.onosproject.gnmi.api.GnmiClient;
Yi Tseng890dc3f2018-11-01 13:23:11 -070031import org.onosproject.gnmi.api.GnmiClientKey;
32import org.onosproject.grpc.ctl.AbstractGrpcClient;
33import org.slf4j.Logger;
Yi Tseng890dc3f2018-11-01 13:23:11 -070034
35import java.util.concurrent.CompletableFuture;
36
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Implementation of gNMI client.
41 */
42public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient {
Yi Tseng5f7fef52018-11-05 11:30:47 -080043 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 Tseng890dc3f2018-11-01 13:23:11 -070046 private final Logger log = getLogger(getClass());
47 private final gNMIGrpc.gNMIBlockingStub blockingStub;
48
Yi Tsengd7716482018-10-31 15:34:30 -070049 GnmiClientImpl(GnmiClientKey clientKey, ManagedChannel managedChannel) {
50 super(clientKey);
Yi Tseng890dc3f2018-11-01 13:23:11 -070051 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 Tseng5f7fef52018-11-05 11:30:47 -080069 @Override
70 public CompletableFuture<Boolean> isServiceAvailable() {
71 return supplyInContext(this::doServiceAvailable, "isServiceAvailable");
72 }
73
Yi Tseng890dc3f2018-11-01 13:23:11 -070074 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 Tseng5f7fef52018-11-05 11:30:47 -0800101
102 private boolean doServiceAvailable() {
103 try {
Yi Tsengd7716482018-10-31 15:34:30 -0700104 return blockingStub.get(DUMMY_REQUEST) != null;
Yi Tseng5f7fef52018-11-05 11:30:47 -0800105 } 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 Tseng890dc3f2018-11-01 13:23:11 -0700113}