blob: 8106a39ef6e765d326bbf50d8bdd7e2758f698d7 [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> {
24
25 private final Dpid dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070026 private final OnosInstanceId onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070027 private final Role role;
28
29 /**
30 * Default constructor for Serializer to use.
31 */
32 @Deprecated
33 protected MastershipEvent() {
34 dpid = null;
35 onosInstanceId = null;
36 role = Role.SLAVE; // Default role is SLAVE
37 }
38
39 /**
40 * Creates the Switch Mastership object.
41 *
42 * @param dpid the Switch DPID
43 * @param onosInstanceId the ONOS Instance ID
44 * @param role the ONOS instance role for the switch.
45 */
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 /**
54 * Creates an unfrozen copy of given Object.
55 *
56 * @param original to make copy of.
57 */
58 public MastershipEvent(MastershipEvent original) {
59 super(original);
60 this.dpid = original.dpid;
61 this.onosInstanceId = original.onosInstanceId;
62 this.role = original.role;
63 }
64
65 /**
66 * Gets the Switch DPID.
67 *
68 * @return the Switch DPID.
69 */
70 public Dpid getDpid() {
71 return dpid;
72 }
73
74 /**
75 * Gets the ONOS Instance ID.
76 *
77 * @return the ONOS Instance ID.
78 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070079 public OnosInstanceId getOnosInstanceId() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070080 return onosInstanceId;
81 }
82
83 /**
84 * Gets the ONOS Controller Role for the Switch.
85 *
86 * @return the ONOS Controller Role for the Switch.
87 */
88 public Role getRole() {
89 return role;
90 }
91
92 @Override
93 public String toString() {
94 return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
95 "->" + getRole() + "]";
96 }
97
98 public byte[] getID() {
99 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
100 return keyStr.getBytes(StandardCharsets.UTF_8);
101 }
102
103 public ByteBuffer getIDasByteBuffer() {
104 ByteBuffer buf = ByteBuffer.wrap(getID());
105 return buf;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(dpid, onosInstanceId);
111 }
112
113 /**
114 * Compares two MastershipEvent objects.
115 * MastershipEvent objects are equal if they have same DPID and same
116 * ONOS Instance ID.
117 *
118 * @param obj another object to compare to this
Yuta HIGUCHId0f68012014-07-28 11:47:12 -0700119 * @return true if equal, false otherwise false.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700120 */
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) {
124 return true;
125 }
126
127 if (obj == null) {
128 return false;
129 }
130
131 if (getClass() != obj.getClass()) {
132 return false;
133 }
134
135 // compare attributes
136 if (!super.equals(obj)) {
137 return false;
138 }
139
140 MastershipEvent other = (MastershipEvent) obj;
141 return dpid.equals(other.dpid) &&
142 onosInstanceId.equals(other.onosInstanceId);
143 }
144}