blob: d1ee1b8a52d0e25f0f75a74318480fff9bc7ed2a [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
80 return getFutureWithDeadline(client.isServiceAvailable(), "getting availability", false);
81 }
82
83 @Override
84 public CompletableFuture<Boolean> disconnect() {
85 final GnmiController controller = handler().get(GnmiController.class);
86 final DeviceId deviceId = handler().data().deviceId();
87 final GnmiClient client = controller.getClient(deviceId);
88 if (client == null) {
89 return CompletableFuture.completedFuture(true);
90 }
91 return client.shutdown()
92 .thenApplyAsync(v -> {
93 controller.removeClient(deviceId);
94 return true;
95 });
96 }
97}