blob: 2b0d82b9535d021925ceadae5dcf23c5bdb7a81f [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 */
Andrea Campanella80b296d2018-09-18 10:48:15 +020052 SUSPENDED,
53
54 /**
55 * Signifies that the underlying storage for the Mastership state
56 * of this device became available again.
57 */
58 RESTORED
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070059 }
60
Jordan Halterman0a2bd452018-06-13 17:24:58 -070061 private final MastershipInfo mastershipInfo;
62
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070063 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070064 * Creates an event of a given type and for the specified device,
65 * role information, and the current time.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070066 *
Jordan Halterman0a2bd452018-06-13 17:24:58 -070067 * @param type mastership event type
68 * @param device event device subject
69 * @param mastershipInfo mastership info
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070070 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070071 public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070072 super(type, device);
Jordan Halterman0a2bd452018-06-13 17:24:58 -070073 this.mastershipInfo = mastershipInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070074 }
75
76 /**
77 * Creates an event of a given type and for the specified device, master,
78 * and time.
79 *
Jordan Halterman0a2bd452018-06-13 17:24:58 -070080 * @param type mastership event type
81 * @param device event device subject
82 * @param mastershipInfo mastership information
83 * @param time occurrence time
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070084 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -070085 public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo, long time) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070086 super(type, device, time);
Jordan Halterman0a2bd452018-06-13 17:24:58 -070087 this.mastershipInfo = mastershipInfo;
88 }
89
90 /**
91 * Returns the mastership info.
92 *
93 * @return the mastership info
94 */
95 public MastershipInfo mastershipInfo() {
96 return mastershipInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070097 }
98
99 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700100 * Returns the current role state for the subject.
101 *
102 * @return RoleInfo associated with Device ID subject
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700103 * @deprecated since 1.14
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700104 */
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700105 @Deprecated
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700106 public RoleInfo roleInfo() {
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700107 return new RoleInfo(mastershipInfo.master().orElse(null), mastershipInfo.backups());
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700108 }
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700109
110 @Override
Jon Hall672a3ed2017-04-05 13:03:37 -0700111 public int hashCode() {
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700112 return Objects.hash(type(), subject(), mastershipInfo(), time());
Jon Hall672a3ed2017-04-05 13:03:37 -0700113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj instanceof MastershipEvent) {
121 final MastershipEvent other = (MastershipEvent) obj;
122 return Objects.equals(this.type(), other.type()) &&
123 Objects.equals(this.subject(), other.subject()) &&
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700124 Objects.equals(this.mastershipInfo(), other.mastershipInfo()) &&
Jon Hall672a3ed2017-04-05 13:03:37 -0700125 Objects.equals(this.time(), other.time());
126 }
127 return false;
128 }
129
130 @Override
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700131 public String toString() {
132 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700133 .add("time", Tools.defaultOffsetDataTime(time()))
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700134 .add("type", type())
135 .add("subject", subject())
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700136 .add("mastershipInfo", mastershipInfo())
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700137 .toString();
138 }
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700139}