Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.nio.charset.StandardCharsets; |
| 5 | import java.util.Objects; |
| 6 | |
| 7 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
| 8 | import net.onrc.onos.core.topology.web.serializers.MastershipEventSerializer; |
| 9 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.OnosInstanceId; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 11 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 12 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 13 | import 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) |
| 23 | public class MastershipEvent extends TopologyElement<MastershipEvent> { |
| 24 | |
| 25 | private final Dpid dpid; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 26 | private final OnosInstanceId onosInstanceId; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 27 | 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 Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 46 | public MastershipEvent(Dpid dpid, OnosInstanceId onosInstanceId, |
| 47 | Role role) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 48 | this.dpid = checkNotNull(dpid); |
| 49 | this.onosInstanceId = checkNotNull(onosInstanceId); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 50 | 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 Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 79 | public OnosInstanceId getOnosInstanceId() { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 80 | 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 Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 92 | /** |
| 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 Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 101 | @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 HIGUCHI | d0f6801 | 2014-07-28 11:47:12 -0700 | [diff] [blame] | 128 | * @return true if equal, false otherwise false. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 129 | */ |
| 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 | } |