Pushed Switch Mastership Events into the Topology event channel.
Needed for ONOS-1729
For now those events are not used.

Also, added new TopologyElement attribute type: TYPE_ALL_LAYERS
which applies to the Mastership Events.

NOTE: Currently, those events are intercepted within the
Floodlight Controller.java class. The interception point might be moved
once it becomes clear whether the event origin should be the mastership
election mechanism or the "role change request" accepted by the switch.
In addition, the interception point needs to be moved/ported as appropriate
once we move to the newer 1.3 OpenFlow driver implementation and eventloop.

Change-Id: Iff06ed5aee867c428a8378e31f9d51dbe3e6b978
diff --git a/src/main/java/net/onrc/onos/core/topology/MastershipEvent.java b/src/main/java/net/onrc/onos/core/topology/MastershipEvent.java
new file mode 100644
index 0000000..01323ba
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/MastershipEvent.java
@@ -0,0 +1,145 @@
+package net.onrc.onos.core.topology;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Objects;
+
+import net.floodlightcontroller.core.IFloodlightProviderService.Role;
+import net.onrc.onos.core.topology.web.serializers.MastershipEventSerializer;
+import net.onrc.onos.core.util.Dpid;
+
+import org.apache.commons.lang.Validate;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+
+/**
+ * Self-contained Switch Mastership event Object.
+ * <p/>
+ * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
+ * FIXME: Current implementation directly use this object as
+ *        Replication message, but should be sending update operation info.
+ */
+@JsonSerialize(using = MastershipEventSerializer.class)
+public class MastershipEvent extends TopologyElement<MastershipEvent> {
+
+    private final Dpid dpid;
+    private final String onosInstanceId;
+    private final Role role;
+
+    /**
+     * Default constructor for Serializer to use.
+     */
+    @Deprecated
+    protected MastershipEvent() {
+        dpid = null;
+        onosInstanceId = null;
+        role = Role.SLAVE;              // Default role is SLAVE
+    }
+
+    /**
+     * Creates the Switch Mastership object.
+     *
+     * @param dpid the Switch DPID
+     * @param onosInstanceId the ONOS Instance ID
+     * @param role the ONOS instance role for the switch.
+     */
+    public MastershipEvent(Dpid dpid, String onosInstanceId, Role role) {
+        Validate.notNull(dpid);
+        Validate.notNull(onosInstanceId);
+
+        this.dpid = dpid;
+        this.onosInstanceId = onosInstanceId;
+        this.role = role;
+    }
+
+    /**
+     * Creates an unfrozen copy of given Object.
+     *
+     * @param original to make copy of.
+     */
+    public MastershipEvent(MastershipEvent original) {
+        super(original);
+        this.dpid = original.dpid;
+        this.onosInstanceId = original.onosInstanceId;
+        this.role = original.role;
+    }
+
+    /**
+     * Gets the Switch DPID.
+     *
+     * @return the Switch DPID.
+     */
+    public Dpid getDpid() {
+        return dpid;
+    }
+
+    /**
+     * Gets the ONOS Instance ID.
+     *
+     * @return the ONOS Instance ID.
+     */
+    public String getOnosInstanceId() {
+        return onosInstanceId;
+    }
+
+    /**
+     * Gets the ONOS Controller Role for the Switch.
+     *
+     * @return the ONOS Controller Role for the Switch.
+     */
+    public Role getRole() {
+        return role;
+    }
+
+    @Override
+    public String toString() {
+        return "[MastershipEvent " + getDpid() + "@" + getOnosInstanceId() +
+            "->" + getRole() + "]";
+    }
+
+    public byte[] getID() {
+        String keyStr = "M" + getDpid() + "@" + getOnosInstanceId();
+        return keyStr.getBytes(StandardCharsets.UTF_8);
+    }
+
+    public ByteBuffer getIDasByteBuffer() {
+        ByteBuffer buf = ByteBuffer.wrap(getID());
+        return buf;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(dpid, onosInstanceId);
+    }
+
+    /**
+     * Compares two MastershipEvent objects.
+     * MastershipEvent objects are equal if they have same DPID and same
+     * ONOS Instance ID.
+     *
+     * @param obj another object to compare to this
+     * @returns true if equal, false otherwise false.
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+
+        // compare attributes
+        if (!super.equals(obj)) {
+            return false;
+        }
+
+        MastershipEvent other = (MastershipEvent) obj;
+        return dpid.equals(other.dpid) &&
+            onosInstanceId.equals(other.onosInstanceId);
+    }
+}