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; |
| 10 | |
| 11 | import org.apache.commons.lang.Validate; |
| 12 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 13 | |
| 14 | /** |
| 15 | * Self-contained Switch Mastership event Object. |
| 16 | * <p/> |
| 17 | * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot? |
| 18 | * FIXME: Current implementation directly use this object as |
| 19 | * Replication message, but should be sending update operation info. |
| 20 | */ |
| 21 | @JsonSerialize(using = MastershipEventSerializer.class) |
| 22 | public class MastershipEvent extends TopologyElement<MastershipEvent> { |
| 23 | |
| 24 | private final Dpid dpid; |
| 25 | private final String onosInstanceId; |
| 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 | /** |
| 39 | * Creates the Switch Mastership object. |
| 40 | * |
| 41 | * @param dpid the Switch DPID |
| 42 | * @param onosInstanceId the ONOS Instance ID |
| 43 | * @param role the ONOS instance role for the switch. |
| 44 | */ |
| 45 | public MastershipEvent(Dpid dpid, String onosInstanceId, Role role) { |
| 46 | Validate.notNull(dpid); |
| 47 | Validate.notNull(onosInstanceId); |
| 48 | |
| 49 | this.dpid = dpid; |
| 50 | this.onosInstanceId = onosInstanceId; |
| 51 | this.role = role; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates an unfrozen copy of given Object. |
| 56 | * |
| 57 | * @param original to make copy of. |
| 58 | */ |
| 59 | public MastershipEvent(MastershipEvent original) { |
| 60 | super(original); |
| 61 | this.dpid = original.dpid; |
| 62 | this.onosInstanceId = original.onosInstanceId; |
| 63 | this.role = original.role; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets the Switch DPID. |
| 68 | * |
| 69 | * @return the Switch DPID. |
| 70 | */ |
| 71 | public Dpid getDpid() { |
| 72 | return dpid; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets the ONOS Instance ID. |
| 77 | * |
| 78 | * @return the ONOS Instance ID. |
| 79 | */ |
| 80 | public String getOnosInstanceId() { |
| 81 | return onosInstanceId; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the ONOS Controller Role for the Switch. |
| 86 | * |
| 87 | * @return the ONOS Controller Role for the Switch. |
| 88 | */ |
| 89 | public Role getRole() { |
| 90 | return role; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() + |
| 96 | "->" + getRole() + "]"; |
| 97 | } |
| 98 | |
| 99 | public byte[] getID() { |
| 100 | String keyStr = "M" + getDpid() + "@" + getOnosInstanceId(); |
| 101 | return keyStr.getBytes(StandardCharsets.UTF_8); |
| 102 | } |
| 103 | |
| 104 | public ByteBuffer getIDasByteBuffer() { |
| 105 | ByteBuffer buf = ByteBuffer.wrap(getID()); |
| 106 | return buf; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int hashCode() { |
| 111 | return Objects.hash(dpid, onosInstanceId); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Compares two MastershipEvent objects. |
| 116 | * MastershipEvent objects are equal if they have same DPID and same |
| 117 | * ONOS Instance ID. |
| 118 | * |
| 119 | * @param obj another object to compare to this |
| 120 | * @returns true if equal, false otherwise false. |
| 121 | */ |
| 122 | @Override |
| 123 | public boolean equals(Object obj) { |
| 124 | if (this == obj) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (obj == null) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (getClass() != obj.getClass()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // compare attributes |
| 137 | if (!super.equals(obj)) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | MastershipEvent other = (MastershipEvent) obj; |
| 142 | return dpid.equals(other.dpid) && |
| 143 | onosInstanceId.equals(other.onosInstanceId); |
| 144 | } |
| 145 | } |