blob: 33f8db86bf2f200083c315076b3200f85d6c1018 [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 Radoslavovd7b792e2014-08-01 02:47:47 -0700112 /**
113 * Gets the event origin DPID.
114 *
115 * @return the event origin DPID.
116 */
117 public Dpid getOriginDpid() {
118 // TODO: Eventually, we should return a collection of Dpid values
119 for (SwitchPort sp : attachmentPoints) {
120 return sp.getDpid();
121 }
122 return null;
123 }
124
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800125 @Override
126 public String toString() {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700127 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800128 }
129
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700130 @Override
131 public int hashCode() {
132 final int prime = 31;
133 int result = super.hashCode();
134 result = prime * result
135 + ((attachmentPoints == null) ? 0 : attachmentPoints.hashCode());
136 result = prime * result + ((mac == null) ? 0 : mac.hashCode());
137 return result;
138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
145
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700146 if (obj == null) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700147 return false;
148 }
149
150 if (getClass() != obj.getClass()) {
151 return false;
152 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700153
154 if (!super.equals(obj)) {
155 return false;
156 }
157
Ray Milkey92897212014-07-21 10:33:16 -0700158 HostEvent other = (HostEvent) obj;
159
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700160 // XXX lastSeenTime excluded from Equality condition, is it OK?
161 return Objects.equals(mac, other.mac) &&
162 Objects.equals(this.attachmentPoints, other.attachmentPoints);
163 }
164
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800165 // Assuming mac is unique cluster-wide
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700166 public static ByteBuffer getHostID(final byte[] mac) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700167 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700168 .putChar('H').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800169 }
170
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800171 public byte[] getID() {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700172 return getHostID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800173 }
174
175 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700176 return getHostID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800177 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800178}