blob: 36a497187d14132f70876a7a8fbafb03220ff7a8 [file] [log] [blame]
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +02001/*
2 * Copyright 2019-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.gnoi;
18
19import org.onosproject.gnoi.api.GnoiClient;
20import org.onosproject.gnoi.api.GnoiClientKey;
21import org.onosproject.gnoi.api.GnoiController;
22import org.onosproject.net.MastershipRole;
23import org.onosproject.net.device.DeviceHandshaker;
24
25import java.util.concurrent.CompletableFuture;
26
27import static java.util.concurrent.CompletableFuture.completedFuture;
28
29/**
30 * Implementation of DeviceHandshaker for gNOI.
31 */
32public class GnoiHandshaker extends AbstractGnoiHandlerBehaviour implements DeviceHandshaker {
33
34 @Override
35 public boolean isReachable() {
36 final GnoiClient client = getClientByKey();
37 return client != null && client.isServerReachable();
38 }
39
40 @Override
41 public CompletableFuture<Boolean> probeReachability() {
42 final GnoiClient 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();
57 }
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() {
71 return CompletableFuture.supplyAsync(this::createClient);
72 }
73
74 private boolean createClient() {
75 GnoiClientKey clientKey = clientKey();
76 if (clientKey == null) {
77 return false;
78 }
79 if (!handler().get(GnoiController.class).createClient(clientKey)) {
80 log.warn("Unable to create client for {}",
81 handler().data().deviceId());
82 return false;
83 }
84 return true;
85 }
86
87 @Override
88 public boolean isConnected() {
89 return getClientByKey() != null;
90 }
91
92 @Override
93 public void disconnect() {
94 handler().get(GnoiController.class)
95 .removeClient(handler().data().deviceId());
96 }
97}