blob: c35764e48f53d270e35552abfdf1662af0c00bf7 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07004import java.util.ArrayList;
5import java.util.Collections;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08006import java.util.LinkedList;
7import java.util.List;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07008import java.util.Objects;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009
10import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070011import net.onrc.onos.core.topology.web.serializers.HostEventSerializer;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070012import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070014import org.codehaus.jackson.map.annotate.JsonSerialize;
15
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016/**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070017 * Self-contained Host event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070018 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070019 * Host event differ from other events.
20 * Host Event represent add/remove of attachmentPoint.
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080021 * Not add/remove of the DeviceObject itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070022 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080023 * Multiple attachmentPoints can be specified to batch events into 1 object.
24 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070025 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070026 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 * FIXME: Current implementation directly use this object as
28 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080029 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070030@JsonSerialize(using = HostEventSerializer.class)
31public class HostEvent extends TopologyElement<HostEvent> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070032
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080033 private final MACAddress mac;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070034 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070035 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036
37 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080038 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080039 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080040 @Deprecated
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070041 protected HostEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070042 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080043 }
44
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070045 public HostEvent(MACAddress mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070046 if (mac == null) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070047 throw new IllegalArgumentException("Host mac cannot be null");
Ray Milkey269ffb92014-04-03 14:43:30 -070048 }
49 this.mac = mac;
50 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080051 }
52
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070053 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070054 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070055 *
56 * @param original to make copy of.
57 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070058 public HostEvent(HostEvent original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 super(original);
60 this.mac = original.mac;
61 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070062 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 }
64
65
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080066 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070067 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080068 }
69
70 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070071 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
74 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070075 if (isFrozen()) {
76 throw new IllegalStateException("Tried to modify frozen instance: " + this);
77 }
Ray Milkey269ffb92014-04-03 14:43:30 -070078 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080079 }
80
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080081 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070082 if (isFrozen()) {
83 throw new IllegalStateException("Tried to modify frozen instance: " + this);
84 }
85 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -070086 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080087 }
88
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070089 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
90 if (isFrozen()) {
91 throw new IllegalStateException("Tried to modify frozen instance: " + this);
92 }
93 return this.attachmentPoints.remove(attachmentPoint);
94 }
95
96
97 public long getLastSeenTime() {
98 return lastSeenTime;
99 }
100
101 public void setLastSeenTime(long lastSeenTime) {
102 if (isFrozen()) {
103 throw new IllegalStateException("Tried to modify frozen instance: " + this);
104 }
105 this.lastSeenTime = lastSeenTime;
106 }
107
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800108 @Override
109 public String toString() {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700110 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800111 }
112
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700113 @Override
114 public int hashCode() {
115 final int prime = 31;
116 int result = super.hashCode();
117 result = prime * result
118 + ((attachmentPoints == null) ? 0 : attachmentPoints.hashCode());
119 result = prime * result + ((mac == null) ? 0 : mac.hashCode());
120 return result;
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 if (this == obj) {
126 return true;
127 }
128
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700129 if (obj == null) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700130 return false;
131 }
132
133 if (getClass() != obj.getClass()) {
134 return false;
135 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700136
137 if (!super.equals(obj)) {
138 return false;
139 }
140
Ray Milkey92897212014-07-21 10:33:16 -0700141 HostEvent other = (HostEvent) obj;
142
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700143 // XXX lastSeenTime excluded from Equality condition, is it OK?
144 return Objects.equals(mac, other.mac) &&
145 Objects.equals(this.attachmentPoints, other.attachmentPoints);
146 }
147
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800148 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800149 public static ByteBuffer getDeviceID(final byte[] mac) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700150 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
151 .putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800152 }
153
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800154 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 return getDeviceID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800156 }
157
158 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700159 return getDeviceID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800160 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800161}