blob: f23a9ca62228a7b4c99ea85a4422a1d41bdfce3f [file] [log] [blame]
Pavlin Radoslavov695f8952014-07-23 16:57:01 -07001package net.onrc.onos.core.topology;
2
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
5import java.io.Serializable;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -07006import java.nio.ByteBuffer;
7import java.nio.charset.StandardCharsets;
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -07008import java.util.Comparator;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -07009import java.util.Objects;
10
11import net.floodlightcontroller.core.IFloodlightProviderService.Role;
12import net.onrc.onos.core.topology.web.serializers.MastershipEventSerializer;
13import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070014import net.onrc.onos.core.util.OnosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070015
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070016import org.codehaus.jackson.map.annotate.JsonSerialize;
17
18/**
19 * Self-contained Switch Mastership event Object.
20 * <p/>
21 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
22 * FIXME: Current implementation directly use this object as
23 * Replication message, but should be sending update operation info.
24 */
25@JsonSerialize(using = MastershipEventSerializer.class)
26public class MastershipEvent extends TopologyElement<MastershipEvent> {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070027 private final Dpid dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070028 private final OnosInstanceId onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070029 private final Role role;
30
31 /**
32 * Default constructor for Serializer to use.
33 */
34 @Deprecated
35 protected MastershipEvent() {
36 dpid = null;
37 onosInstanceId = null;
38 role = Role.SLAVE; // Default role is SLAVE
39 }
40
41 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070042 * Constructor for given switch DPID, ONOS Instance ID, and ONOS instance
43 * role for the switch.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070044 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070045 * @param dpid the switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070046 * @param onosInstanceId the ONOS Instance ID
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070047 * @param role the ONOS instance role for the switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070048 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070049 public MastershipEvent(Dpid dpid, OnosInstanceId onosInstanceId,
50 Role role) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070051 this.dpid = checkNotNull(dpid);
52 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070053 this.role = role;
54 }
55
56 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070057 * Copy constructor.
58 * <p>
59 * Creates an unfrozen copy of the given Switch MastershipEvent object.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070060 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070061 * @param original the object to make copy of
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070062 */
63 public MastershipEvent(MastershipEvent original) {
64 super(original);
65 this.dpid = original.dpid;
66 this.onosInstanceId = original.onosInstanceId;
67 this.role = original.role;
68 }
69
70 /**
71 * Gets the Switch DPID.
72 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070073 * @return the Switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070074 */
75 public Dpid getDpid() {
76 return dpid;
77 }
78
79 /**
80 * Gets the ONOS Instance ID.
81 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070082 * @return the ONOS Instance ID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070083 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070084 public OnosInstanceId getOnosInstanceId() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070085 return onosInstanceId;
86 }
87
88 /**
89 * Gets the ONOS Controller Role for the Switch.
90 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070091 * @return the ONOS Controller Role for the Switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070092 */
93 public Role getRole() {
94 return role;
95 }
96
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070097 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070098 public Dpid getOriginDpid() {
99 return this.dpid;
100 }
101
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700102 @Override
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700103 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700104 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
105 byte[] id = keyStr.getBytes(StandardCharsets.UTF_8);
106 ByteBuffer buf = ByteBuffer.wrap(id);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700107 return buf;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(dpid, onosInstanceId);
113 }
114
115 /**
116 * Compares two MastershipEvent objects.
117 * MastershipEvent objects are equal if they have same DPID and same
118 * ONOS Instance ID.
119 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700120 * @param o another object to compare to this
121 * @return true if equal, false otherwise
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700122 */
123 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700124 public boolean equals(Object o) {
125 if (this == o) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700126 return true;
127 }
128
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700129 if (o == null || getClass() != o.getClass()) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700130 return false;
131 }
132
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700133 // Compare attributes
134 if (!super.equals(o)) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700135 return false;
136 }
137
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700138 MastershipEvent other = (MastershipEvent) o;
139 return Objects.equals(dpid, other.dpid) &&
140 Objects.equals(onosInstanceId, other.onosInstanceId);
141 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700142
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700143 /**
144 * Comparator to bring {@link MastershipEvent} with MASTER role up front.
145 * <p>
146 * Note: expected to be used against Collection of MastershipEvents
147 * about same Dpid.
148 */
149 public static final class MasterFirstComparator
150 implements Comparator<MastershipEvent>, Serializable {
151 @Override
152 public int compare(MastershipEvent o1, MastershipEvent o2) {
153 // MastershipEvent for same ONOS Instance
154 // => treat as equal regardless of Role
155 // Note: MastershipEvent#equals() does not use Role
156 if (o1.equals(o2)) {
157 return 0;
158 }
159
160 // MASTER Role instance is considered smaller.
161 // (appears earlier in SortedSet iteration, etc.)
162 if (o1.getRole() == Role.MASTER) {
163 return -1;
164 }
165 if (o2.getRole() == Role.MASTER) {
166 return 1;
167 }
168
169 return o1.getOnosInstanceId().toString()
170 .compareTo(o2.getOnosInstanceId().toString());
171 }
172 }
173
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700174 @Override
175 public String toString() {
176 return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
177 "->" + getRole() + "]";
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700178 }
179}