blob: 86042b01bbd0b1791a7c4b8978f02894564b719d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.mastership;
tomca90c462014-09-22 11:40:58 -070017
Madan Jampani565a66a2015-07-25 17:01:13 -070018import static org.onosproject.net.MastershipRole.MASTER;
Jian Li25fc9ad2016-05-03 17:35:44 -070019import static org.onosproject.net.MastershipRole.NONE;
Madan Jampani565a66a2015-07-25 17:01:13 -070020
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070021import java.util.Set;
Madan Jampanide003d92015-05-11 17:14:20 -070022import java.util.concurrent.CompletableFuture;
Jian Li25fc9ad2016-05-03 17:35:44 -070023import java.util.concurrent.TimeUnit;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070024
Jian Li25fc9ad2016-05-03 17:35:44 -070025import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.NodeId;
27import org.onosproject.cluster.RoleInfo;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070028import org.onosproject.event.ListenerService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DeviceId;
30import org.onosproject.net.MastershipRole;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070031
tomca90c462014-09-22 11:40:58 -070032/**
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 Vachuska42e8cce2015-07-29 19:25:18 -070038public interface MastershipService
39 extends ListenerService<MastershipEvent, MastershipListener> {
tomca90c462014-09-22 11:40:58 -070040
Jian Li25fc9ad2016-05-03 17:35:44 -070041 long TIMEOUT_MILLIS = 3000;
42
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070043 /**
tomb41d1ac2014-09-24 01:51:24 -070044 * Returns the role of the local node for the specified device, without
45 * triggering master selection.
46 *
Jian Li25fc9ad2016-05-03 17:35:44 -070047 * @param deviceId the identifier of the device
tomb41d1ac2014-09-24 01:51:24 -070048 * @return role of the current node
49 */
50 MastershipRole getLocalRole(DeviceId deviceId);
51
52 /**
Madan Jampani565a66a2015-07-25 17:01:13 -070053 * Returns true if the local controller is the Master for the specified deviceId.
54 *
Jian Li25fc9ad2016-05-03 17:35:44 -070055 * @param deviceId the identifier of the device
Madan Jampani565a66a2015-07-25 17:01:13 -070056 * @return true if local node is master; false otherwise
57 */
58 default boolean isLocalMaster(DeviceId deviceId) {
59 return getLocalRole(deviceId) == MASTER;
60 }
61
62 /**
tomb41d1ac2014-09-24 01:51:24 -070063 * Returns the mastership status of the local controller for a given
64 * device forcing master selection if necessary.
65 *
Jian Li25fc9ad2016-05-03 17:35:44 -070066 * @param deviceId the identifier of the device
67 * @return future object of this controller instance role
tomb41d1ac2014-09-24 01:51:24 -070068 */
Madan Jampanide003d92015-05-11 17:14:20 -070069 CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId);
tomb41d1ac2014-09-24 01:51:24 -070070
71 /**
Jian Li25fc9ad2016-05-03 17:35:44 -070072 * 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 /**
tomb41d1ac2014-09-24 01:51:24 -070084 * 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 Koshibec4047702014-10-07 14:43:52 -070086 * for this device, no master selection will occur.
tomb41d1ac2014-09-24 01:51:24 -070087 *
88 * @param deviceId the identifier of the device
Madan Jampanic6e574f2015-05-29 13:41:52 -070089 * @return future that is completed when relinquish is complete
tomb41d1ac2014-09-24 01:51:24 -070090 */
Madan Jampanic6e574f2015-05-29 13:41:52 -070091 CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
tomb41d1ac2014-09-24 01:51:24 -070092
93 /**
Jian Li25fc9ad2016-05-03 17:35:44 -070094 * 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 Koshibe8b3270f2014-09-22 13:44:42 -0700106 * 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 */
tome4729872014-09-23 00:37:37 -0700111 NodeId getMasterFor(DeviceId deviceId);
tomca90c462014-09-22 11:40:58 -0700112
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700113 /**
Ayaka Koshibe45503ce2014-10-14 11:26:45 -0700114 * 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 Koshibeabedb092014-10-20 17:01:31 -0700120 RoleInfo getNodesFor(DeviceId deviceId);
Ayaka Koshibe45503ce2014-10-14 11:26:45 -0700121
122 /**
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700123 * Returns the devices for which a controller is master.
Yuta HIGUCHId9340032017-01-25 09:25:44 -0800124 * <p>
125 * Returned Set may contain DeviceId which no longer exist in the system.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700126 *
tome4729872014-09-23 00:37:37 -0700127 * @param nodeId the ID of the controller
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700128 * @return a set of device IDs
Madan Jampani39b3b192016-05-19 08:08:29 -0700129 * @deprecated 1.6.0 Goldeneye release.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700130 */
Madan Jampani39b3b192016-05-19 08:08:29 -0700131 @Deprecated
tome4729872014-09-23 00:37:37 -0700132 Set<DeviceId> getDevicesOf(NodeId nodeId);
tomca90c462014-09-22 11:40:58 -0700133
tomca90c462014-09-22 11:40:58 -0700134}