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