blob: 45578db1f9706caa3a436a98cf7a452457cc3a99 [file] [log] [blame]
Pavlin Radoslavov695f8952014-07-23 16:57:01 -07001package net.onrc.onos.core.topology;
2
3import java.nio.ByteBuffer;
4import java.nio.charset.StandardCharsets;
5import java.util.Objects;
6
7import net.floodlightcontroller.core.IFloodlightProviderService.Role;
8import net.onrc.onos.core.topology.web.serializers.MastershipEventSerializer;
9import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070010import net.onrc.onos.core.util.OnosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070011
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070012import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070013import org.codehaus.jackson.map.annotate.JsonSerialize;
14
15/**
16 * Self-contained Switch Mastership event Object.
17 * <p/>
18 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
19 * FIXME: Current implementation directly use this object as
20 * Replication message, but should be sending update operation info.
21 */
22@JsonSerialize(using = MastershipEventSerializer.class)
23public class MastershipEvent extends TopologyElement<MastershipEvent> {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070024 private final Dpid dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070025 private final OnosInstanceId onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070026 private final Role role;
27
28 /**
29 * Default constructor for Serializer to use.
30 */
31 @Deprecated
32 protected MastershipEvent() {
33 dpid = null;
34 onosInstanceId = null;
35 role = Role.SLAVE; // Default role is SLAVE
36 }
37
38 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070039 * Constructor for given switch DPID, ONOS Instance ID, and ONOS instance
40 * role for the switch.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070041 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070042 * @param dpid the switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070043 * @param onosInstanceId the ONOS Instance ID
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070044 * @param role the ONOS instance role for the switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070045 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070046 public MastershipEvent(Dpid dpid, OnosInstanceId onosInstanceId,
47 Role role) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070048 this.dpid = checkNotNull(dpid);
49 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070050 this.role = role;
51 }
52
53 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070054 * Copy constructor.
55 * <p>
56 * Creates an unfrozen copy of the given Switch MastershipEvent object.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070057 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070058 * @param original the object to make copy of
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070059 */
60 public MastershipEvent(MastershipEvent original) {
61 super(original);
62 this.dpid = original.dpid;
63 this.onosInstanceId = original.onosInstanceId;
64 this.role = original.role;
65 }
66
67 /**
68 * Gets the Switch DPID.
69 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070070 * @return the Switch DPID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070071 */
72 public Dpid getDpid() {
73 return dpid;
74 }
75
76 /**
77 * Gets the ONOS Instance ID.
78 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070079 * @return the ONOS Instance ID
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070080 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070081 public OnosInstanceId getOnosInstanceId() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070082 return onosInstanceId;
83 }
84
85 /**
86 * Gets the ONOS Controller Role for the Switch.
87 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070088 * @return the ONOS Controller Role for the Switch
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070089 */
90 public Role getRole() {
91 return role;
92 }
93
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070094 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070095 public Dpid getOriginDpid() {
96 return this.dpid;
97 }
98
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070099 @Override
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700100 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700101 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
102 byte[] id = keyStr.getBytes(StandardCharsets.UTF_8);
103 ByteBuffer buf = ByteBuffer.wrap(id);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700104 return buf;
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(dpid, onosInstanceId);
110 }
111
112 /**
113 * Compares two MastershipEvent objects.
114 * MastershipEvent objects are equal if they have same DPID and same
115 * ONOS Instance ID.
116 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700117 * @param o another object to compare to this
118 * @return true if equal, false otherwise
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700119 */
120 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700121 public boolean equals(Object o) {
122 if (this == o) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700123 return true;
124 }
125
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700126 if (o == null || getClass() != o.getClass()) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700127 return false;
128 }
129
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700130 // Compare attributes
131 if (!super.equals(o)) {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700132 return false;
133 }
134
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700135 MastershipEvent other = (MastershipEvent) o;
136 return Objects.equals(dpid, other.dpid) &&
137 Objects.equals(onosInstanceId, other.onosInstanceId);
138 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700139
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700140 @Override
141 public String toString() {
142 return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
143 "->" + getRole() + "]";
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700144 }
145}