blob: 35c32e798fc84be093acc7016e51363e2c33aa57 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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'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 */
47 BACKUPS_CHANGED
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070048 }
49
50 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070051 * Creates an event of a given type and for the specified device,
52 * role information, and the current time.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070053 *
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070054 * @param type mastership event type
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070055 * @param device event device subject
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070056 * @param info mastership role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070057 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070058 public MastershipEvent(Type type, DeviceId device, RoleInfo info) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070059 super(type, device);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070060 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070061 }
62
63 /**
64 * Creates an event of a given type and for the specified device, master,
65 * and time.
66 *
67 * @param type mastership event type
68 * @param device event device subject
Thomas Vachuska4b420772014-10-30 16:46:17 -070069 * @param info role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070070 * @param time occurrence time
71 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070072 public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070073 super(type, device, time);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070074 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070075 }
76
77 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070078 * Returns the current role state for the subject.
79 *
80 * @return RoleInfo associated with Device ID subject
81 */
82 public RoleInfo roleInfo() {
83 return roleInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070084 }
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070085
86 @Override
87 public String toString() {
88 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070089 .add("time", new LocalDateTime(time()))
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070090 .add("type", type())
91 .add("subject", subject())
92 .add("roleInfo", roleInfo)
93 .toString();
94 }
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070095}