blob: 87470793005af0af982be7d6a92eed66b04783a4 [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);
62 }
63
64
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080065 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080067 }
68
69 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070070 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080071 }
72
73 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 if (isFrozen()) {
75 throw new IllegalStateException("Tried to modify frozen instance: " + this);
76 }
Ray Milkey269ffb92014-04-03 14:43:30 -070077 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080078 }
79
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080080 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070081 if (isFrozen()) {
82 throw new IllegalStateException("Tried to modify frozen instance: " + this);
83 }
84 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -070085 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080086 }
87
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070088 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
89 if (isFrozen()) {
90 throw new IllegalStateException("Tried to modify frozen instance: " + this);
91 }
92 return this.attachmentPoints.remove(attachmentPoint);
93 }
94
95
96 public long getLastSeenTime() {
97 return lastSeenTime;
98 }
99
100 public void setLastSeenTime(long lastSeenTime) {
101 if (isFrozen()) {
102 throw new IllegalStateException("Tried to modify frozen instance: " + this);
103 }
104 this.lastSeenTime = lastSeenTime;
105 }
106
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800107 @Override
108 public String toString() {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700109 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800110 }
111
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700112 @Override
113 public int hashCode() {
114 final int prime = 31;
115 int result = super.hashCode();
116 result = prime * result
117 + ((attachmentPoints == null) ? 0 : attachmentPoints.hashCode());
118 result = prime * result + ((mac == null) ? 0 : mac.hashCode());
119 return result;
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700128 if (obj == null) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700129 return false;
130 }
131
132 if (getClass() != obj.getClass()) {
133 return false;
134 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700135 HostEvent other = (HostEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700136
137 if (!super.equals(obj)) {
138 return false;
139 }
140
141 // XXX lastSeenTime excluded from Equality condition, is it OK?
142 return Objects.equals(mac, other.mac) &&
143 Objects.equals(this.attachmentPoints, other.attachmentPoints);
144 }
145
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800146 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800147 public static ByteBuffer getDeviceID(final byte[] mac) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700148 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
149 .putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800150 }
151
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800152 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 return getDeviceID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800154 }
155
156 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 return getDeviceID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800158 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800159}