blob: 1c64939c9339e6f65f73ce4704f622ed6bf52953 [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 Radoslavova5637c02014-07-30 15:55:11 -070014import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070015import org.codehaus.jackson.map.annotate.JsonSerialize;
16
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080017/**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070018 * Self-contained Host event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070019 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070020 * Host event differ from other events.
21 * Host Event represent add/remove of attachmentPoint.
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070022 * Not add/remove of the Host Object itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070023 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080024 * Multiple attachmentPoints can be specified to batch events into 1 object.
25 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070026 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070027 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070028 * FIXME: Current implementation directly use this object as
29 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080030 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070031@JsonSerialize(using = HostEventSerializer.class)
32public class HostEvent extends TopologyElement<HostEvent> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070033
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080034 private final MACAddress mac;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070035 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070036 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080037
38 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080039 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080040 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080041 @Deprecated
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070042 protected HostEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070043 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080044 }
45
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070046 /**
47 * Creates the Host object.
48 *
49 * @param mac the MAC address to identify the host
50 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070051 public HostEvent(MACAddress mac) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070052 this.mac = checkNotNull(mac);
Ray Milkey269ffb92014-04-03 14:43:30 -070053 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080054 }
55
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070057 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058 *
59 * @param original to make copy of.
60 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070061 public HostEvent(HostEvent original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070062 super(original);
63 this.mac = original.mac;
64 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070065 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 }
67
68
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080069 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080071 }
72
73 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080075 }
76
77 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070078 if (isFrozen()) {
79 throw new IllegalStateException("Tried to modify frozen instance: " + this);
80 }
Ray Milkey269ffb92014-04-03 14:43:30 -070081 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080082 }
83
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080084 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070085 if (isFrozen()) {
86 throw new IllegalStateException("Tried to modify frozen instance: " + this);
87 }
88 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -070089 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080090 }
91
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
93 if (isFrozen()) {
94 throw new IllegalStateException("Tried to modify frozen instance: " + this);
95 }
96 return this.attachmentPoints.remove(attachmentPoint);
97 }
98
99
100 public long getLastSeenTime() {
101 return lastSeenTime;
102 }
103
104 public void setLastSeenTime(long lastSeenTime) {
105 if (isFrozen()) {
106 throw new IllegalStateException("Tried to modify frozen instance: " + this);
107 }
108 this.lastSeenTime = lastSeenTime;
109 }
110
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800111 @Override
112 public String toString() {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700113 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800114 }
115
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700116 @Override
117 public int hashCode() {
118 final int prime = 31;
119 int result = super.hashCode();
120 result = prime * result
121 + ((attachmentPoints == null) ? 0 : attachmentPoints.hashCode());
122 result = prime * result + ((mac == null) ? 0 : mac.hashCode());
123 return result;
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (this == obj) {
129 return true;
130 }
131
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700132 if (obj == null) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700133 return false;
134 }
135
136 if (getClass() != obj.getClass()) {
137 return false;
138 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700139
140 if (!super.equals(obj)) {
141 return false;
142 }
143
Ray Milkey92897212014-07-21 10:33:16 -0700144 HostEvent other = (HostEvent) obj;
145
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700146 // XXX lastSeenTime excluded from Equality condition, is it OK?
147 return Objects.equals(mac, other.mac) &&
148 Objects.equals(this.attachmentPoints, other.attachmentPoints);
149 }
150
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800151 // Assuming mac is unique cluster-wide
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700152 public static ByteBuffer getHostID(final byte[] mac) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700153 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700154 .putChar('H').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800155 }
156
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800157 public byte[] getID() {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700158 return getHostID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800159 }
160
161 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700162 return getHostID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800163 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800164}