blob: cdae99c28864e3b1b436d9ebf2d54beb460feb7e [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08002
weibitcef09862014-07-11 17:05:16 -07003import java.util.Objects;
4
5
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08006/**
7 * Self-contained Topology event Object
Ray Milkey269ffb92014-04-03 14:43:30 -07008 * <p/>
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08009 * TODO: For now the topology event contains one of the following events:
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070010 * Switch, Port, Link, Host, Switch Mastership. In the future it will contain
11 * multiple events in a single transaction.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080012 */
13public class TopologyEvent {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070014 SwitchEvent switchEvent = null; // Set for Switch event
15 PortEvent portEvent = null; // Set for Port event
16 LinkEvent linkEvent = null; // Set for Link event
17 HostEvent hostEvent = null; // Set for Host event
18 MastershipEvent mastershipEvent = null; // Set for Mastership event
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080019
20 /**
21 * Default constructor.
22 */
23 public TopologyEvent() {
24 }
25
26 /**
27 * Constructor for given Switch event.
28 *
29 * @param switchEvent the Switch event to use.
30 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080031 TopologyEvent(SwitchEvent switchEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070032 this.switchEvent = switchEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080033 }
34
35 /**
36 * Constructor for given Port event.
37 *
38 * @param portEvent the Port event to use.
39 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080040 TopologyEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070041 this.portEvent = portEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080042 }
43
44 /**
45 * Constructor for given Link event.
46 *
47 * @param linkEvent the Link event to use.
48 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080049 TopologyEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 this.linkEvent = linkEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080051 }
52
53 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070054 * Constructor for given Host event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080055 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070056 * @param hostEvent the Host event to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080057 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070058 TopologyEvent(HostEvent hostEvent) {
59 this.hostEvent = hostEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080060 }
61
62 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070063 * Constructor for given Switch Mastership event.
64 *
65 * @param mastershipEvent the Switch Mastership event to use.
66 */
67 TopologyEvent(MastershipEvent mastershipEvent) {
68 this.mastershipEvent = mastershipEvent;
69 }
70
71 /**
weibitcef09862014-07-11 17:05:16 -070072 * Check if all events contained are equal.
Yuta HIGUCHIccab05d2014-07-26 22:42:28 -070073 *
74 * @param obj TopologyEvent to compare against
weibitcef09862014-07-11 17:05:16 -070075 */
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81
82 if (obj == null) {
83 return false;
84 }
85
86 if (getClass() != obj.getClass()) {
87 return false;
88 }
89
90 TopologyEvent other = (TopologyEvent) obj;
91 return Objects.equals(switchEvent, other.switchEvent) &&
92 Objects.equals(portEvent, other.portEvent) &&
93 Objects.equals(linkEvent, other.linkEvent) &&
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070094 Objects.equals(hostEvent, other.hostEvent) &&
95 Objects.equals(mastershipEvent, other.mastershipEvent);
weibitcef09862014-07-11 17:05:16 -070096 }
97
98 @Override
99 public int hashCode() {
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700100 return Objects.hash(switchEvent, portEvent, linkEvent, hostEvent,
101 mastershipEvent);
weibitcef09862014-07-11 17:05:16 -0700102 }
103
104 /**
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800105 * Get the string representation of the event.
106 *
107 * @return the string representation of the event.
108 */
109 @Override
110 public String toString() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700111 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 return switchEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700113 }
114 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 return portEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700116 }
117 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 return linkEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700119 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700120 if (hostEvent != null) {
121 return hostEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700123 if (mastershipEvent != null) {
124 return mastershipEvent.toString();
125 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 return "[Empty TopologyEvent]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800127 }
128
129 /**
130 * Get the Topology event ID.
131 *
132 * @return the Topology event ID.
133 */
134 public byte[] getID() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700135 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 return switchEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700137 }
138 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 return portEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700140 }
141 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 return linkEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700143 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700144 if (hostEvent != null) {
145 return hostEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700146 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700147 if (mastershipEvent != null) {
148 return mastershipEvent.getID();
149 }
Ray Milkey92897212014-07-21 10:33:16 -0700150 throw new IllegalStateException("Invalid TopologyEvent ID");
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800151 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800152}