blob: 589174d7f483b82d22cf990c5c108e2e17e27470 [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
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070092 /**
93 * Gets the event origin DPID.
94 *
95 * @return the event origin DPID.
96 */
97 public Dpid getOriginDpid() {
98 return this.dpid;
99 }
100
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700101 @Override
102 public String toString() {
103 return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
104 "->" + getRole() + "]";
105 }
106
107 public byte[] getID() {
108 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
109 return keyStr.getBytes(StandardCharsets.UTF_8);
110 }
111
112 public ByteBuffer getIDasByteBuffer() {
113 ByteBuffer buf = ByteBuffer.wrap(getID());
114 return buf;
115 }
116
117 @Override
118 public int hashCode() {
119 return Objects.hash(dpid, onosInstanceId);
120 }
121
122 /**
123 * Compares two MastershipEvent objects.
124 * MastershipEvent objects are equal if they have same DPID and same
125 * ONOS Instance ID.
126 *
127 * @param obj another object to compare to this
Yuta HIGUCHId0f68012014-07-28 11:47:12 -0700128 * @return true if equal, false otherwise false.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700129 */
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135
136 if (obj == null) {
137 return false;
138 }
139
140 if (getClass() != obj.getClass()) {
141 return false;
142 }
143
144 // compare attributes
145 if (!super.equals(obj)) {
146 return false;
147 }
148
149 MastershipEvent other = (MastershipEvent) obj;
150 return dpid.equals(other.dpid) &&
151 onosInstanceId.equals(other.onosInstanceId);
152 }
153}