Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 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.net.device; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onosproject.net.MastershipRole; |
| 21 | import org.onosproject.net.driver.DeviceConnect; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 22 | import org.onosproject.net.provider.ProviderId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.concurrent.CompletableFuture; |
| 25 | |
| 26 | /** |
| 27 | * Behavior to test device's reachability and change the mastership role on that |
| 28 | * device. |
| 29 | */ |
| 30 | @Beta |
| 31 | public interface DeviceHandshaker extends DeviceConnect { |
| 32 | |
| 33 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 34 | * Returns true if this node is presumed to be able to send messages and |
| 35 | * receive replies from the device. |
| 36 | * <p> |
| 37 | * The implementation should not make any attempt at actively probing the |
| 38 | * device over the network, as such it should not block execution. Instead, |
| 39 | * it should return a result based solely on internal state (e.g. socket |
| 40 | * state). If it returns true, then this node is expected to communicate |
| 41 | * with the server successfully. In other words, if any message would be |
| 42 | * sent to the device immediately after this method is called and returns |
| 43 | * true, then such message is expected, but NOT guaranteed, to reach the |
| 44 | * device. If false, it means communication with the device is unlikely to |
| 45 | * happen soon. |
| 46 | * <p> |
| 47 | * Some implementations might require a connection to be created via {@link |
| 48 | * #connect()} before checking for reachability. Similarly, after invoking |
| 49 | * {@link #disconnect()}, this method might always return false. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 50 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 51 | * @return true if the device is deemed reachable, false otherwise |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 52 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 53 | boolean isReachable(); |
| 54 | |
| 55 | /** |
| 56 | * Similar to {@link #isReachable()}, but performs probing of the device |
| 57 | * over the network. This method should be called if {@link #isReachable()} |
| 58 | * returns false and the caller wants to be sure this is not a transient |
| 59 | * failure state by actively probing the device. |
| 60 | * |
| 61 | * @return completable future eventually true if device responded to probe, |
| 62 | * false otherwise |
| 63 | */ |
| 64 | CompletableFuture<Boolean> probeReachability(); |
| 65 | |
| 66 | /** |
| 67 | * Checks the availability of the device. Availability denotes whether the |
| 68 | * device is reachable and able to perform its functions as expected (e.g., |
| 69 | * forward traffic). Similar to {@link #isReachable()}, implementations are |
| 70 | * not allowed to probe the device over the network, but the result should |
| 71 | * be based solely on internal state. |
| 72 | * <p> |
| 73 | * Implementation of this method is optional. If not supported, an exception |
| 74 | * should be thrown. |
| 75 | * |
| 76 | * @return true if the device is deemed available, false otherwise |
| 77 | * @throws UnsupportedOperationException if this method is not supported and |
| 78 | * {@link #probeAvailability()} should |
| 79 | * be used instead. |
| 80 | */ |
| 81 | boolean isAvailable(); |
| 82 | |
| 83 | /** |
| 84 | * Similar to {@link #isAvailable()} but allows probing the device over the |
| 85 | * network. Differently from {@link #isAvailable()}, implementation of this |
| 86 | * method is mandatory. |
| 87 | * |
| 88 | * @return completable future eventually true if available, false otherwise |
| 89 | */ |
| 90 | CompletableFuture<Boolean> probeAvailability(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 93 | * Notifies the device a mastership role change as decided by the core. The |
| 94 | * implementation of this method should trigger a {@link DeviceAgentEvent} |
| 95 | * signaling the mastership role accepted by the device. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 96 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 97 | * @param newRole new mastership role |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 98 | * @throws UnsupportedOperationException if the device does not support |
| 99 | * mastership handling |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 100 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 101 | void roleChanged(MastershipRole newRole); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 102 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 103 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 104 | * Notifies the device of a mastership role change as decided by the core. |
| 105 | * Differently from {@link #roleChanged(MastershipRole)}, the role is |
| 106 | * described by the given preference value, where {@code preference = 0} |
| 107 | * signifies {@link MastershipRole#MASTER} role and {@code preference > 0} |
| 108 | * signifies {@link MastershipRole#STANDBY}. Smaller preference values |
| 109 | * indicates higher mastership priority for different nodes. |
| 110 | * <p> |
| 111 | * This method does not permit notifying role {@link MastershipRole#NONE}, |
| 112 | * in which case {@link #roleChanged(MastershipRole)} should be used |
| 113 | * instead. |
| 114 | * <p> |
| 115 | * Term is a monotonically increasing number, increased by one every time a |
| 116 | * new master is elected. |
| 117 | * <p> |
| 118 | * The implementation of this method should trigger a {@link |
| 119 | * DeviceAgentEvent} signaling the mastership role accepted by the device. |
| 120 | * |
| 121 | * @param preference preference value, where 0 signifies {@link |
| 122 | * MastershipRole#MASTER} and all other values {@link |
| 123 | * MastershipRole#STANDBY} |
| 124 | * @param term term number |
| 125 | * @throws UnsupportedOperationException if the device does not support |
| 126 | * mastership handling, or if it does |
| 127 | * not support setting preference-based |
| 128 | * mastership, and {@link #roleChanged(MastershipRole)} |
| 129 | * should be used instead |
| 130 | */ |
| 131 | default void roleChanged(int preference, long term) { |
| 132 | if (preference == 0) { |
| 133 | roleChanged(MastershipRole.MASTER); |
| 134 | } else { |
| 135 | roleChanged(MastershipRole.STANDBY); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 140 | * Returns the last known mastership role agreed by the device for this |
| 141 | * node. |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 142 | * |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 143 | * @return mastership role |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 144 | */ |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 145 | MastershipRole getRole(); |
| 146 | |
| 147 | /** |
| 148 | * Adds a device agent listener for the given provider ID. |
| 149 | * |
| 150 | * @param providerId provider ID |
| 151 | * @param listener device agent listener |
| 152 | */ |
| 153 | default void addDeviceAgentListener( |
| 154 | ProviderId providerId, DeviceAgentListener listener) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 155 | throw new UnsupportedOperationException( |
| 156 | "Device agent listener registration not supported"); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 160 | * Removes a device agent listener previously registered for the given |
| 161 | * provider ID. |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 162 | * |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 163 | * @param providerId provider ID |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 164 | */ |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 165 | default void removeDeviceAgentListener(ProviderId providerId) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 166 | throw new UnsupportedOperationException( |
| 167 | "Device agent listener removal not supported"); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 170 | } |