blob: 5668e2f02b6898c7b26799cb6b67ebb4d1836531 [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
12import org.apache.commons.lang.Validate;
13import 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 Radoslavov695f8952014-07-23 16:57:01 -070048 Validate.notNull(dpid);
49 Validate.notNull(onosInstanceId);
50
51 this.dpid = dpid;
52 this.onosInstanceId = onosInstanceId;
53 this.role = role;
54 }
55
56 /**
57 * Creates an unfrozen copy of given Object.
58 *
59 * @param original to make copy of.
60 */
61 public MastershipEvent(MastershipEvent original) {
62 super(original);
63 this.dpid = original.dpid;
64 this.onosInstanceId = original.onosInstanceId;
65 this.role = original.role;
66 }
67
68 /**
69 * Gets the Switch DPID.
70 *
71 * @return the Switch DPID.
72 */
73 public Dpid getDpid() {
74 return dpid;
75 }
76
77 /**
78 * Gets the ONOS Instance ID.
79 *
80 * @return the ONOS Instance ID.
81 */
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070082 public OnosInstanceId getOnosInstanceId() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070083 return onosInstanceId;
84 }
85
86 /**
87 * Gets the ONOS Controller Role for the Switch.
88 *
89 * @return the ONOS Controller Role for the Switch.
90 */
91 public Role getRole() {
92 return role;
93 }
94
95 @Override
96 public String toString() {
97 return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
98 "->" + getRole() + "]";
99 }
100
101 public byte[] getID() {
102 String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
103 return keyStr.getBytes(StandardCharsets.UTF_8);
104 }
105
106 public ByteBuffer getIDasByteBuffer() {
107 ByteBuffer buf = ByteBuffer.wrap(getID());
108 return buf;
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hash(dpid, onosInstanceId);
114 }
115
116 /**
117 * Compares two MastershipEvent objects.
118 * MastershipEvent objects are equal if they have same DPID and same
119 * ONOS Instance ID.
120 *
121 * @param obj another object to compare to this
Yuta HIGUCHId0f68012014-07-28 11:47:12 -0700122 * @return true if equal, false otherwise false.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700123 */
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129
130 if (obj == null) {
131 return false;
132 }
133
134 if (getClass() != obj.getClass()) {
135 return false;
136 }
137
138 // compare attributes
139 if (!super.equals(obj)) {
140 return false;
141 }
142
143 MastershipEvent other = (MastershipEvent) obj;
144 return dpid.equals(other.dpid) &&
145 onosInstanceId.equals(other.onosInstanceId);
146 }
147}