blob: 9942f05dcd27e005f71e9926c1e822cd4cac6b1d [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;
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070012import net.onrc.onos.core.topology.web.serializers.MastershipDataSerializer;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070013import 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/**
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070019 * Self-contained Switch Mastership object.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070020 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070021@JsonSerialize(using = MastershipDataSerializer.class)
22public class MastershipData extends TopologyElement<MastershipData> {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070023 private final Dpid dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070024 private final OnosInstanceId onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070025 private final Role role;
26
27 /**
28 * Default constructor for Serializer to use.
29 */
30 @Deprecated
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070031 protected MastershipData() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070032 dpid = null;
33 onosInstanceId = null;
34 role = Role.SLAVE; // Default role is SLAVE
35 }
36
37 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070038 * Constructor for given switch DPID, ONOS Instance ID, and ONOS instance
39 * role for the switch.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070040 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070041 * @param dpid the switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070042 * @param onosInstanceId the ONOS Instance ID
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070043 * @param role the ONOS instance role for the switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070044 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070045 public MastershipData(Dpid dpid, OnosInstanceId onosInstanceId,
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070046 Role role) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070047 this.dpid = checkNotNull(dpid);
48 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070049 this.role = role;
50 }
51
52 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070053 * Copy constructor.
54 * <p>
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070055 * Creates an unfrozen copy of the given Switch MastershipData object.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070056 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070057 * @param original the object to make copy of
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070058 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070059 public MastershipData(MastershipData original) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070060 super(original);
61 this.dpid = original.dpid;
62 this.onosInstanceId = original.onosInstanceId;
63 this.role = original.role;
64 }
65
66 /**
67 * Gets the Switch DPID.
68 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070069 * @return the Switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070070 */
71 public Dpid getDpid() {
72 return dpid;
73 }
74
75 /**
76 * Gets the ONOS Instance ID.
77 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070078 * @return the ONOS Instance ID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070079 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070080 public OnosInstanceId getOnosInstanceId() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070081 return onosInstanceId;
82 }
83
84 /**
85 * Gets the ONOS Controller Role for the Switch.
86 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070087 * @return the ONOS Controller Role for the Switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070088 */
89 public Role getRole() {
90 return role;
91 }
92
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070093 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070094 public Dpid getOriginDpid() {
95 return this.dpid;
96 }
97
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070098 @Override
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070099 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700100 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
101 byte[] id = keyStr.getBytes(StandardCharsets.UTF_8);
102 ByteBuffer buf = ByteBuffer.wrap(id);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700103 return buf;
104 }
105
106 @Override
107 public int hashCode() {
108 return Objects.hash(dpid, onosInstanceId);
109 }
110
111 /**
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700112 * Compares two MastershipData objects.
113 * MastershipData objects are equal if they have same DPID and same
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700114 * ONOS Instance ID.
115 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700116 * @param o another object to compare to this
117 * @return true if equal, false otherwise
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700118 */
119 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700120 public boolean equals(Object o) {
121 if (this == o) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700122 return true;
123 }
124
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700125 if (o == null || getClass() != o.getClass()) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700126 return false;
127 }
128
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700129 // Compare attributes
130 if (!super.equals(o)) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700131 return false;
132 }
133
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700134 MastershipData other = (MastershipData) o;
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700135 return Objects.equals(dpid, other.dpid) &&
136 Objects.equals(onosInstanceId, other.onosInstanceId);
137 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700138
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700139 /**
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700140 * Comparator to bring {@link MastershipData} with MASTER role up front.
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700141 * <p>
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700142 * Note: expected to be used against Collection of MastershipData
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700143 * about same Dpid.
144 */
145 public static final class MasterFirstComparator
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700146 implements Comparator<MastershipData>, Serializable {
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700147 @Override
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700148 public int compare(MastershipData o1, MastershipData o2) {
149 // MastershipData for same ONOS Instance
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700150 // => treat as equal regardless of Role
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700151 // Note: MastershipData#equals() does not use Role
Yuta HIGUCHIa5c48bc2014-08-27 10:34:15 -0700152 if (o1.equals(o2)) {
153 return 0;
154 }
155
156 // MASTER Role instance is considered smaller.
157 // (appears earlier in SortedSet iteration, etc.)
158 if (o1.getRole() == Role.MASTER) {
159 return -1;
160 }
161 if (o2.getRole() == Role.MASTER) {
162 return 1;
163 }
164
165 return o1.getOnosInstanceId().toString()
166 .compareTo(o2.getOnosInstanceId().toString());
167 }
168 }
169
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700170 @Override
171 public String toString() {
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700172 return "[MastershipData " + getDpid() + "@" + getOnosInstanceId() +
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700173 "->" + getRole() + "]";
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700174 }
175}