blob: 7b41f85fe2d37c54f10b31f03ec9fd6fb66b0f0b [file] [log] [blame]
Yi Tsengd7716482018-10-31 15:34:30 -07001/*
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.stratum;
18
Yi Tsengd7716482018-10-31 15:34:30 -070019import org.onosproject.drivers.gnmi.GnmiHandshaker;
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020020import org.onosproject.drivers.gnoi.GnoiHandshaker;
Yi Tsengd7716482018-10-31 15:34:30 -070021import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker;
Yi Tsengd7716482018-10-31 15:34:30 -070022import org.onosproject.net.MastershipRole;
23import org.onosproject.net.device.DeviceAgentListener;
24import org.onosproject.net.device.DeviceHandshaker;
Yi Tsengd7716482018-10-31 15:34:30 -070025import org.onosproject.net.provider.ProviderId;
Yi Tsengd7716482018-10-31 15:34:30 -070026
27import java.util.concurrent.CompletableFuture;
Yi Tsengd7716482018-10-31 15:34:30 -070028
29/**
30 * Implementation of DeviceHandshaker for Stratum device.
31 */
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080032public class StratumHandshaker
33 extends AbstractStratumBehaviour<DeviceHandshaker>
34 implements DeviceHandshaker {
Yi Tsengd7716482018-10-31 15:34:30 -070035
36 public StratumHandshaker() {
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020037 super(new P4RuntimeHandshaker(), new GnmiHandshaker(), new GnoiHandshaker());
Yi Tsengd7716482018-10-31 15:34:30 -070038 }
39
40 @Override
Carmelo Casconec2be50a2019-04-10 00:15:39 -070041 public boolean connect() {
42 return p4runtime.connect() && gnmi.connect() && gnoi.connect();
43 }
44
45 @Override
46 public boolean hasConnection() {
47 return p4runtime.hasConnection() && gnmi.hasConnection() && gnoi.hasConnection();
48 }
49
50 @Override
51 public void disconnect() {
52 p4runtime.disconnect();
53 gnmi.disconnect();
54 gnoi.disconnect();
55 }
56
57 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080058 public boolean isReachable() {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080059 // Reachability is mainly used for mastership contests and it's a
60 // prerequisite for availability. We can probably live without gNMI and
61 // gNOI, but we will always need P4Runtime.
62 return p4runtime.isReachable();
Carmelo Cascone3977ea42019-02-28 13:43:42 -080063 }
64
65 @Override
66 public CompletableFuture<Boolean> probeReachability() {
pierventre1c18ee52022-02-22 15:43:46 -080067 // p4runtime probe reachability is based on GetPipelineConfig gRPC that
68 // can timeout if we are setting in parallel the pipeline: the two requests
69 // can concur for the same lock. For our purposes it is enough to check if
70 // the device is still there; for this reason stratum handshaker now relies
71 // on gNOI reachability which is based on getTime RPC.
72 return gnoi.probeReachability();
Carmelo Cascone3977ea42019-02-28 13:43:42 -080073 }
74
75 @Override
76 public boolean isAvailable() {
77 // Availability concerns packet forwarding, hence we consider only
78 // P4Runtime.
79 return p4runtime.isAvailable();
80 }
81
82 @Override
83 public CompletableFuture<Boolean> probeAvailability() {
84 return p4runtime.probeAvailability();
Yi Tsengd7716482018-10-31 15:34:30 -070085 }
86
87 @Override
88 public void roleChanged(MastershipRole newRole) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080089 p4runtime.roleChanged(newRole);
90 }
91
92 @Override
93 public void roleChanged(int preference, long term) {
94 p4runtime.roleChanged(preference, term);
Yi Tsengd7716482018-10-31 15:34:30 -070095 }
96
97 @Override
98 public MastershipRole getRole() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080099 return p4runtime.getRole();
Yi Tsengd7716482018-10-31 15:34:30 -0700100 }
101
102 @Override
103 public void addDeviceAgentListener(ProviderId providerId, DeviceAgentListener listener) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800104 p4runtime.addDeviceAgentListener(providerId, listener);
Yi Tsengd7716482018-10-31 15:34:30 -0700105 }
106
107 @Override
108 public void removeDeviceAgentListener(ProviderId providerId) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800109 p4runtime.removeDeviceAgentListener(providerId);
Yi Tsengd7716482018-10-31 15:34:30 -0700110 }
Yi Tsengd7716482018-10-31 15:34:30 -0700111}