blob: c4434601ade485f4f4c5858298b975c4c5c27951 [file] [log] [blame]
Yi Tseng5f7fef52018-11-05 11:30:47 -08001/*
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
17package org.onosproject.drivers.gnmi;
18
19import org.onosproject.gnmi.api.GnmiClient;
20import org.onosproject.gnmi.api.GnmiController;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.MastershipRole;
23import org.onosproject.net.device.DeviceHandshaker;
24
25import java.util.concurrent.CompletableFuture;
26
27/**
28 * Implementation of DeviceHandshaker for gNMI.
29 */
30public class GnmiHandshaker extends AbstractGnmiHandlerBehaviour implements DeviceHandshaker {
31
32 @Override
33 public CompletableFuture<Boolean> isReachable() {
34 return CompletableFuture
35 // gNMI requires a client to be created to
36 // check for reachability.
37 .supplyAsync(super::createClient)
38 .thenApplyAsync(client -> {
39 if (client == null) {
40 return false;
41 }
42 return handler()
43 .get(GnmiController.class)
44 .isReachable(handler().data().deviceId());
45 });
46 }
47
48 @Override
49 public void roleChanged(MastershipRole newRole) {
50 throw new UnsupportedOperationException("Mastership operation not supported");
51 }
52
53 @Override
54 public MastershipRole getRole() {
55 throw new UnsupportedOperationException("Mastership operation not supported");
56 }
57
58 @Override
59 public CompletableFuture<Boolean> connect() {
60 return CompletableFuture
61 .supplyAsync(super::createClient)
62 .thenComposeAsync(client -> {
63 if (client == null) {
64 return CompletableFuture.completedFuture(false);
65 }
66 return CompletableFuture.completedFuture(true);
67 });
68 }
69
70 @Override
71 public boolean isConnected() {
72 final GnmiController controller = handler().get(GnmiController.class);
73 final DeviceId deviceId = handler().data().deviceId();
74 final GnmiClient client = controller.getClient(deviceId);
75
76 if (client == null) {
77 return false;
78 }
79
Yi Tsengd7716482018-10-31 15:34:30 -070080 return getFutureWithDeadline(
81 client.isServiceAvailable(),
82 "checking if gNMI service is available", false);
Yi Tseng5f7fef52018-11-05 11:30:47 -080083 }
84
85 @Override
86 public CompletableFuture<Boolean> disconnect() {
87 final GnmiController controller = handler().get(GnmiController.class);
88 final DeviceId deviceId = handler().data().deviceId();
89 final GnmiClient client = controller.getClient(deviceId);
90 if (client == null) {
91 return CompletableFuture.completedFuture(true);
92 }
93 return client.shutdown()
94 .thenApplyAsync(v -> {
95 controller.removeClient(deviceId);
96 return true;
97 });
98 }
99}