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; |
| 20 | import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 21 | import org.onosproject.net.MastershipRole; |
| 22 | import org.onosproject.net.device.DeviceAgentListener; |
| 23 | import org.onosproject.net.device.DeviceHandshaker; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 24 | import org.onosproject.net.provider.ProviderId; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Implementation of DeviceHandshaker for Stratum device. |
| 30 | */ |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 31 | public class StratumHandshaker |
| 32 | extends AbstractStratumBehaviour<DeviceHandshaker> |
| 33 | implements DeviceHandshaker { |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 34 | |
| 35 | public StratumHandshaker() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 36 | super(new P4RuntimeHandshaker(), new GnmiHandshaker()); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 40 | public boolean isReachable() { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 41 | // Reachability is mainly used for mastership contests and it's a |
| 42 | // prerequisite for availability. We can probably live without gNMI and |
| 43 | // gNOI, but we will always need P4Runtime. |
| 44 | return p4runtime.isReachable(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public CompletableFuture<Boolean> probeReachability() { |
| 49 | return p4runtime.probeReachability(); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean isAvailable() { |
| 54 | // Availability concerns packet forwarding, hence we consider only |
| 55 | // P4Runtime. |
| 56 | return p4runtime.isAvailable(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public CompletableFuture<Boolean> probeAvailability() { |
| 61 | return p4runtime.probeAvailability(); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void roleChanged(MastershipRole newRole) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 66 | p4runtime.roleChanged(newRole); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void roleChanged(int preference, long term) { |
| 71 | p4runtime.roleChanged(preference, term); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public MastershipRole getRole() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 76 | return p4runtime.getRole(); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void addDeviceAgentListener(ProviderId providerId, DeviceAgentListener listener) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 81 | p4runtime.addDeviceAgentListener(providerId, listener); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void removeDeviceAgentListener(ProviderId providerId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 86 | p4runtime.removeDeviceAgentListener(providerId); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public CompletableFuture<Boolean> connect() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 91 | // We should execute connections in parallel. |
| 92 | return p4runtime.connect().thenCombine(gnmi.connect(), Boolean::logicalAnd); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean isConnected() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 97 | return p4runtime.isConnected() && gnmi.isConnected(); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 101 | public void disconnect() { |
| 102 | p4runtime.disconnect(); |
| 103 | gnmi.disconnect(); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 104 | } |
| 105 | } |