Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.stratum; |
| 18 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 19 | import org.onosproject.drivers.gnmi.GnmiHandshaker; |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 20 | import org.onosproject.drivers.gnoi.GnoiHandshaker; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 21 | import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 22 | import org.onosproject.net.MastershipRole; |
| 23 | import org.onosproject.net.device.DeviceAgentListener; |
| 24 | import org.onosproject.net.device.DeviceHandshaker; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 25 | import org.onosproject.net.provider.ProviderId; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Implementation of DeviceHandshaker for Stratum device. |
| 31 | */ |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 32 | public class StratumHandshaker |
| 33 | extends AbstractStratumBehaviour<DeviceHandshaker> |
| 34 | implements DeviceHandshaker { |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 35 | |
| 36 | public StratumHandshaker() { |
oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 37 | super(new P4RuntimeHandshaker(), new GnmiHandshaker(), new GnoiHandshaker()); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 41 | 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 Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 58 | public boolean isReachable() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 59 | // 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 Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Boolean> probeReachability() { |
pierventre | 1c18ee5 | 2022-02-22 15:43:46 -0800 | [diff] [blame^] | 67 | // 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 Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 73 | } |
| 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 Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void roleChanged(MastershipRole newRole) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 89 | p4runtime.roleChanged(newRole); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void roleChanged(int preference, long term) { |
| 94 | p4runtime.roleChanged(preference, term); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public MastershipRole getRole() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 99 | return p4runtime.getRole(); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void addDeviceAgentListener(ProviderId providerId, DeviceAgentListener listener) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 104 | p4runtime.addDeviceAgentListener(providerId, listener); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void removeDeviceAgentListener(ProviderId providerId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 109 | p4runtime.removeDeviceAgentListener(providerId); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 110 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 111 | } |