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 | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 92 | @Override |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 93 | public Dpid getOriginDpid() { |
| 94 | return this.dpid; |
| 95 | } |
| 96 | |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 97 | @Override |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 98 | public ByteBuffer getIDasByteBuffer() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 99 | String keyStr = "M" + getDpid() + "@" + getOnosInstanceId(); |
| 100 | byte[] id = keyStr.getBytes(StandardCharsets.UTF_8); |
| 101 | ByteBuffer buf = ByteBuffer.wrap(id); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 102 | return buf; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int hashCode() { |
| 107 | return Objects.hash(dpid, onosInstanceId); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Compares two MastershipEvent objects. |
| 112 | * MastershipEvent objects are equal if they have same DPID and same |
| 113 | * ONOS Instance ID. |
| 114 | * |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 115 | * @param o another object to compare to this. |
| 116 | * @return true if equal, false otherwise. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 117 | */ |
| 118 | @Override |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 119 | public boolean equals(Object o) { |
| 120 | if (this == o) { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 124 | if (o == null || getClass() != o.getClass()) { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 128 | // Compare attributes |
| 129 | if (!super.equals(o)) { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 133 | MastershipEvent other = (MastershipEvent) o; |
| 134 | return Objects.equals(dpid, other.dpid) && |
| 135 | Objects.equals(onosInstanceId, other.onosInstanceId); |
| 136 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 137 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public String toString() { |
| 140 | return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() + |
| 141 | "->" + getRole() + "]"; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 142 | } |
| 143 | } |