blob: e7c0169939b5360f273e7b3f0e8016734df5e74c [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;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070017
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070018import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.cluster.RoleInfo;
20import org.onosproject.event.AbstractEvent;
21import org.onosproject.net.DeviceId;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070022
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070023import com.google.common.base.MoreObjects;
24
Jon Hall672a3ed2017-04-05 13:03:37 -070025import java.util.Objects;
26
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070027/**
Ayaka Koshibe8d504a92014-09-22 17:07:36 -070028 * Describes a device mastership event.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070029 */
30public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> {
31
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070032 /**
33 * Type of mastership events.
34 */
35 public enum Type {
36 /**
37 * Signifies that the master for a device has changed.
38 */
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070039 MASTER_CHANGED,
40
41 /**
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070042 * Signifies that the list of backup nodes has changed. If
43 * the change in the backups list is accompanied by a change in
44 * master, the event is subsumed by MASTER_CHANGED.
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070045 */
Jon Hall7a8bfc62016-05-26 17:59:04 -070046 BACKUPS_CHANGED,
47
48 /**
49 * Signifies that the underlying storage for the Mastership state
50 * of this device is unavailable.
51 */
52 SUSPENDED
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070053 }
54
Jordan Halterman0a2bd452018-06-13 17:24:58 -070055 private final MastershipInfo mastershipInfo;
56
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070057 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070058 * Creates an event of a given type and for the specified device,
59 * role information, and the current time.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070060 *
Jordan Halterman0a2bd452018-06-13 17:24:58 -070061 * @param type mastership event type
62 * @param device event device subject
63 * @param mastershipInfo mastership info
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070064 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070065 public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070066 super(type, device);
Jordan Halterman0a2bd452018-06-13 17:24:58 -070067 this.mastershipInfo = mastershipInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070068 }
69
70 /**
71 * Creates an event of a given type and for the specified device, master,
72 * and time.
73 *
Jordan Halterman0a2bd452018-06-13 17:24:58 -070074 * @param type mastership event type
75 * @param device event device subject
76 * @param mastershipInfo mastership information
77 * @param time occurrence time
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070078 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070079 public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo, long time) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070080 super(type, device, time);
Jordan Halterman0a2bd452018-06-13 17:24:58 -070081 this.mastershipInfo = mastershipInfo;
82 }
83
84 /**
85 * Returns the mastership info.
86 *
87 * @return the mastership info
88 */
89 public MastershipInfo mastershipInfo() {
90 return mastershipInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070091 }
92
93 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070094 * Returns the current role state for the subject.
95 *
96 * @return RoleInfo associated with Device ID subject
Jordan Halterman0a2bd452018-06-13 17:24:58 -070097 * @deprecated since 1.14
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070098 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070099 @Deprecated
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700100 public RoleInfo roleInfo() {
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700101 return new RoleInfo(mastershipInfo.master().orElse(null), mastershipInfo.backups());
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700102 }
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700103
104 @Override
Jon Hall672a3ed2017-04-05 13:03:37 -0700105 public int hashCode() {
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700106 return Objects.hash(type(), subject(), mastershipInfo(), time());
Jon Hall672a3ed2017-04-05 13:03:37 -0700107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj instanceof MastershipEvent) {
115 final MastershipEvent other = (MastershipEvent) obj;
116 return Objects.equals(this.type(), other.type()) &&
117 Objects.equals(this.subject(), other.subject()) &&
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700118 Objects.equals(this.mastershipInfo(), other.mastershipInfo()) &&
Jon Hall672a3ed2017-04-05 13:03:37 -0700119 Objects.equals(this.time(), other.time());
120 }
121 return false;
122 }
123
124 @Override
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700125 public String toString() {
126 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700127 .add("time", Tools.defaultOffsetDataTime(time()))
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700128 .add("type", type())
129 .add("subject", subject())
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700130 .add("mastershipInfo", mastershipInfo())
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700131 .toString();
132 }
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700133}