Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.mastership; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 18 | import static org.onosproject.net.MastershipRole.MASTER; |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 19 | import static org.onosproject.net.MastershipRole.NONE; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 20 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 21 | import java.util.Set; |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 22 | import java.util.concurrent.CompletableFuture; |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 24 | |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.cluster.RoleInfo; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 28 | import org.onosproject.event.ListenerService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.MastershipRole; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 31 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Service responsible for determining the controller instance mastership of |
| 34 | * a device in a clustered environment. This is the central authority for |
| 35 | * determining mastership, but is not responsible for actually applying it |
| 36 | * to the devices; this falls on the device service. |
| 37 | */ |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 38 | public interface MastershipService |
| 39 | extends ListenerService<MastershipEvent, MastershipListener> { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 40 | |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 41 | long TIMEOUT_MILLIS = 3000; |
| 42 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 43 | /** |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 44 | * Returns the role of the local node for the specified device, without |
| 45 | * triggering master selection. |
| 46 | * |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 47 | * @param deviceId the identifier of the device |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 48 | * @return role of the current node |
| 49 | */ |
| 50 | MastershipRole getLocalRole(DeviceId deviceId); |
| 51 | |
| 52 | /** |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 53 | * Returns true if the local controller is the Master for the specified deviceId. |
| 54 | * |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 55 | * @param deviceId the identifier of the device |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 56 | * @return true if local node is master; false otherwise |
| 57 | */ |
| 58 | default boolean isLocalMaster(DeviceId deviceId) { |
| 59 | return getLocalRole(deviceId) == MASTER; |
| 60 | } |
| 61 | |
| 62 | /** |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 63 | * Returns the mastership status of the local controller for a given |
| 64 | * device forcing master selection if necessary. |
| 65 | * |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 66 | * @param deviceId the identifier of the device |
| 67 | * @return future object of this controller instance role |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 68 | */ |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 69 | CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 72 | * Synchronous version of requestRoleFor. Returns the mastership status of |
| 73 | * the local controller for a given device forcing master selection if necessary. |
| 74 | * |
| 75 | * @param deviceId the identifier of the device |
| 76 | * @return the role of this controller instance |
| 77 | */ |
| 78 | default MastershipRole requestRoleForSync(DeviceId deviceId) { |
| 79 | return Tools.futureGetOrElse(requestRoleFor(deviceId), |
| 80 | TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, NONE); |
| 81 | } |
| 82 | |
| 83 | /** |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 84 | * Abandons mastership of the specified device on the local node thus |
| 85 | * forcing selection of a new master. If the local node is not a master |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 86 | * for this device, no master selection will occur. |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 87 | * |
| 88 | * @param deviceId the identifier of the device |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 89 | * @return future that is completed when relinquish is complete |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 90 | */ |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 91 | CompletableFuture<Void> relinquishMastership(DeviceId deviceId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
Jian Li | 25fc9ad | 2016-05-03 17:35:44 -0700 | [diff] [blame] | 94 | * Synchronous version of relinquishMastership. Abandons mastership of the |
| 95 | * specified device on the local node thus forcing selection of a new master. |
| 96 | * If the local node is not a master for this device, no master selection will occur. |
| 97 | * |
| 98 | * @param deviceId the identifier of the device |
| 99 | */ |
| 100 | default void relinquishMastershipSync(DeviceId deviceId) { |
| 101 | Tools.futureGetOrElse(relinquishMastership(deviceId), |
| 102 | TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null); |
| 103 | } |
| 104 | |
| 105 | /** |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 106 | * Returns the current master for a given device. |
| 107 | * |
| 108 | * @param deviceId the identifier of the device |
| 109 | * @return the ID of the master controller for the device |
| 110 | */ |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 111 | NodeId getMasterFor(DeviceId deviceId); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 112 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 113 | /** |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 114 | * Returns controllers connected to a given device, in order of |
| 115 | * preference. The first entry in the list is the current master. |
| 116 | * |
| 117 | * @param deviceId the identifier of the device |
| 118 | * @return a list of controller IDs |
| 119 | */ |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 120 | RoleInfo getNodesFor(DeviceId deviceId); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 123 | * Returns the mastership info for the given device. |
| 124 | * |
| 125 | * @param deviceId the device for which to return the mastership info |
| 126 | * @return the mastership info for the given device |
| 127 | */ |
| 128 | MastershipInfo getMastershipFor(DeviceId deviceId); |
| 129 | |
| 130 | /** |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 131 | * Returns the devices for which a controller is master. |
Yuta HIGUCHI | d934003 | 2017-01-25 09:25:44 -0800 | [diff] [blame] | 132 | * <p> |
| 133 | * Returned Set may contain DeviceId which no longer exist in the system. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 134 | * |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 135 | * @param nodeId the ID of the controller |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 136 | * @return a set of device IDs |
Madan Jampani | 39b3b19 | 2016-05-19 08:08:29 -0700 | [diff] [blame] | 137 | * @deprecated 1.6.0 Goldeneye release. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 138 | */ |
Madan Jampani | 39b3b19 | 2016-05-19 08:08:29 -0700 | [diff] [blame] | 139 | @Deprecated |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 140 | Set<DeviceId> getDevicesOf(NodeId nodeId); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 141 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 142 | } |