blob: 8a941ca603760719f4ce6ac62a878a010dd0c54b [file] [log] [blame]
Andrea Campanella241896c2017-05-10 13:11:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella241896c2017-05-10 13:11:04 -07003 *
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.net.device;
18
19import com.google.common.annotations.Beta;
20import org.onosproject.net.MastershipRole;
21import org.onosproject.net.driver.DeviceConnect;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070022import org.onosproject.net.provider.ProviderId;
Andrea Campanella241896c2017-05-10 13:11:04 -070023
24import 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
31public interface DeviceHandshaker extends DeviceConnect {
32
33 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080034 * 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 Campanella241896c2017-05-10 13:11:04 -070050 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -080051 * @return true if the device is deemed reachable, false otherwise
Andrea Campanella241896c2017-05-10 13:11:04 -070052 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080053 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 Campanella241896c2017-05-10 13:11:04 -070091
92 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +020093 * 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 Campanella241896c2017-05-10 13:11:04 -070096 *
Carmelo Casconee5b28722018-06-22 17:28:28 +020097 * @param newRole new mastership role
Carmelo Cascone3977ea42019-02-28 13:43:42 -080098 * @throws UnsupportedOperationException if the device does not support
99 * mastership handling
Andrea Campanella241896c2017-05-10 13:11:04 -0700100 */
Carmelo Casconee5b28722018-06-22 17:28:28 +0200101 void roleChanged(MastershipRole newRole);
Andrea Campanella241896c2017-05-10 13:11:04 -0700102
Andrea Campanella1e573442018-05-17 17:07:13 +0200103 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800104 * 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 Cascone9e4972c2018-08-30 00:29:16 -0700140 * Returns the last known mastership role agreed by the device for this
141 * node.
Andrea Campanella1e573442018-05-17 17:07:13 +0200142 *
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700143 * @return mastership role
Andrea Campanella1e573442018-05-17 17:07:13 +0200144 */
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700145 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 Casconee5b28722018-06-22 17:28:28 +0200155 throw new UnsupportedOperationException(
156 "Device agent listener registration not supported");
Andrea Campanella1e573442018-05-17 17:07:13 +0200157 }
158
159 /**
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700160 * Removes a device agent listener previously registered for the given
161 * provider ID.
Andrea Campanella1e573442018-05-17 17:07:13 +0200162 *
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700163 * @param providerId provider ID
Andrea Campanella1e573442018-05-17 17:07:13 +0200164 */
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700165 default void removeDeviceAgentListener(ProviderId providerId) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200166 throw new UnsupportedOperationException(
167 "Device agent listener removal not supported");
Andrea Campanella1e573442018-05-17 17:07:13 +0200168 }
169
Andrea Campanella241896c2017-05-10 13:11:04 -0700170}