blob: 86d17511f953ebb5d0bb08dbb212f6b32a5fc353 [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;
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070012import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070013import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070015import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070016import org.codehaus.jackson.map.annotate.JsonSerialize;
17
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080018/**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070019 * Self-contained Host event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070020 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070021 * Host event differ from other events.
22 * Host Event represent add/remove of attachmentPoint.
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070023 * Not add/remove of the Host Object itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070024 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080025 * Multiple attachmentPoints can be specified to batch events into 1 object.
26 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070027 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070028 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070029 * FIXME: Current implementation directly use this object as
30 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080031 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070032@JsonSerialize(using = HostEventSerializer.class)
33public class HostEvent extends TopologyElement<HostEvent> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070034
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 private final MACAddress mac;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070037 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080038
39 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080040 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080041 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080042 @Deprecated
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070043 protected HostEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080045 }
46
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070047 /**
48 * Creates the Host object.
49 *
50 * @param mac the MAC address to identify the host
51 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070052 public HostEvent(MACAddress mac) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070053 this.mac = checkNotNull(mac);
Ray Milkey269ffb92014-04-03 14:43:30 -070054 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080055 }
56
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070058 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 *
60 * @param original to make copy of.
61 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070062 public HostEvent(HostEvent original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 super(original);
64 this.mac = original.mac;
65 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070066 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070067 }
68
69
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080070 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
74 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070075 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 }
77
78 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070079 if (isFrozen()) {
80 throw new IllegalStateException("Tried to modify frozen instance: " + this);
81 }
Ray Milkey269ffb92014-04-03 14:43:30 -070082 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080083 }
84
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080085 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 if (isFrozen()) {
87 throw new IllegalStateException("Tried to modify frozen instance: " + this);
88 }
89 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -070090 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080091 }
92
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070093 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
94 if (isFrozen()) {
95 throw new IllegalStateException("Tried to modify frozen instance: " + this);
96 }
97 return this.attachmentPoints.remove(attachmentPoint);
98 }
99
100
101 public long getLastSeenTime() {
102 return lastSeenTime;
103 }
104
105 public void setLastSeenTime(long lastSeenTime) {
106 if (isFrozen()) {
107 throw new IllegalStateException("Tried to modify frozen instance: " + this);
108 }
109 this.lastSeenTime = lastSeenTime;
110 }
111
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700112 // Assuming mac is unique cluster-wide
113 public static ByteBuffer getHostID(final byte[] mac) {
114 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
115 .putChar('H').put(mac).flip();
116 }
117
118 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700119 public Dpid getOriginDpid() {
120 // TODO: Eventually, we should return a collection of Dpid values
121 for (SwitchPort sp : attachmentPoints) {
122 return sp.getDpid();
123 }
124 return null;
125 }
126
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800127 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700128 public ByteBuffer getIDasByteBuffer() {
129 return getHostID(mac.toBytes());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800130 }
131
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700132 @Override
133 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700134 return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700135 }
136
137 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700138 public boolean equals(Object o) {
139 if (this == o) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700140 return true;
141 }
142
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700143 if (o == null || getClass() != o.getClass()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700144 return false;
145 }
146
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700147 // Compare attributes
148 if (!super.equals(o)) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700149 return false;
150 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700151
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700152 HostEvent other = (HostEvent) o;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700153 // XXX lastSeenTime excluded from Equality condition, is it OK?
154 return Objects.equals(mac, other.mac) &&
155 Objects.equals(this.attachmentPoints, other.attachmentPoints);
156 }
157
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700158 @Override
159 public String toString() {
160 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800161 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800162}