blob: a6d0c04f77633de54505708e837fe8e99ef71000 [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;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080020import org.onosproject.gnmi.api.GnmiClientKey;
Yi Tseng5f7fef52018-11-05 11:30:47 -080021import org.onosproject.gnmi.api.GnmiController;
Yi Tseng5f7fef52018-11-05 11:30:47 -080022import org.onosproject.net.MastershipRole;
23import org.onosproject.net.device.DeviceHandshaker;
24
25import java.util.concurrent.CompletableFuture;
26
Carmelo Cascone3977ea42019-02-28 13:43:42 -080027import static java.util.concurrent.CompletableFuture.completedFuture;
28
Yi Tseng5f7fef52018-11-05 11:30:47 -080029/**
30 * Implementation of DeviceHandshaker for gNMI.
31 */
32public class GnmiHandshaker extends AbstractGnmiHandlerBehaviour implements DeviceHandshaker {
33
34 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080035 public boolean isReachable() {
36 final GnmiClient client = getClientByKey();
37 return client != null && client.isServerReachable();
38 }
39
40 @Override
41 public CompletableFuture<Boolean> probeReachability() {
42 final GnmiClient client = getClientByKey();
43 if (client == null) {
44 return completedFuture(false);
45 }
46 return client.probeService();
47 }
48
49 @Override
50 public boolean isAvailable() {
51 return isReachable();
52 }
53
54 @Override
55 public CompletableFuture<Boolean> probeAvailability() {
56 return probeReachability();
Yi Tseng5f7fef52018-11-05 11:30:47 -080057 }
58
59 @Override
60 public void roleChanged(MastershipRole newRole) {
61 throw new UnsupportedOperationException("Mastership operation not supported");
62 }
63
64 @Override
65 public MastershipRole getRole() {
66 throw new UnsupportedOperationException("Mastership operation not supported");
67 }
68
69 @Override
70 public CompletableFuture<Boolean> connect() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080071 return CompletableFuture.supplyAsync(this::createClient);
72 }
73
74 private boolean createClient() {
75 GnmiClientKey clientKey = clientKey();
76 if (clientKey == null) {
77 return false;
78 }
79 if (!handler().get(GnmiController.class).createClient(clientKey)) {
80 log.warn("Unable to create client for {}",
81 handler().data().deviceId());
82 return false;
83 }
84 return true;
Yi Tseng5f7fef52018-11-05 11:30:47 -080085 }
86
87 @Override
88 public boolean isConnected() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080089 return getClientByKey() != null;
Yi Tseng5f7fef52018-11-05 11:30:47 -080090 }
91
92 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080093 public void disconnect() {
94 handler().get(GnmiController.class)
95 .removeClient(handler().data().deviceId());
Yi Tseng5f7fef52018-11-05 11:30:47 -080096 }
97}