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