blob: 1156787c4d0ee5b17d7ddfb4443bfa55a5550bb6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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 HIGUCHIb87ef952014-10-28 23:34:23 -070018import org.joda.time.LocalDateTime;
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
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070025/**
Ayaka Koshibe8d504a92014-09-22 17:07:36 -070026 * Describes a device mastership event.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070027 */
28public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> {
29
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070030 //Contains master and standby information.
31 RoleInfo roleInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070032
33 /**
34 * Type of mastership events.
35 */
36 public enum Type {
37 /**
38 * Signifies that the master for a device has changed.
39 */
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070040 MASTER_CHANGED,
41
42 /**
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070043 * Signifies that the list of backup nodes has changed. If
44 * the change in the backups list is accompanied by a change in
45 * master, the event is subsumed by MASTER_CHANGED.
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070046 */
Jon Hall7a8bfc62016-05-26 17:59:04 -070047 BACKUPS_CHANGED,
48
49 /**
50 * Signifies that the underlying storage for the Mastership state
51 * of this device is unavailable.
52 */
53 SUSPENDED
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070054 }
55
56 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070057 * Creates an event of a given type and for the specified device,
58 * role information, and the current time.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070059 *
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070060 * @param type mastership event type
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070061 * @param device event device subject
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070062 * @param info mastership role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070063 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070064 public MastershipEvent(Type type, DeviceId device, RoleInfo info) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070065 super(type, device);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070066 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070067 }
68
69 /**
70 * Creates an event of a given type and for the specified device, master,
71 * and time.
72 *
73 * @param type mastership event type
74 * @param device event device subject
Thomas Vachuska4b420772014-10-30 16:46:17 -070075 * @param info role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070076 * @param time occurrence time
77 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070078 public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070079 super(type, device, time);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070080 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070081 }
82
83 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070084 * Returns the current role state for the subject.
85 *
86 * @return RoleInfo associated with Device ID subject
87 */
88 public RoleInfo roleInfo() {
89 return roleInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070090 }
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070091
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070095 .add("time", new LocalDateTime(time()))
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070096 .add("type", type())
97 .add("subject", subject())
98 .add("roleInfo", roleInfo)
99 .toString();
100 }
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700101}