Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collections; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 8 | import java.util.Objects; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 9 | |
| 10 | import net.floodlightcontroller.util.MACAddress; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.topology.web.serializers.HostEventSerializer; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 13 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame^] | 14 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 15 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 16 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 17 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 18 | * Self-contained Host event(s) Object |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 20 | * Host event differ from other events. |
| 21 | * Host Event represent add/remove of attachmentPoint. |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 22 | * Not add/remove of the Host Object itself. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 23 | * <p/> |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 24 | * Multiple attachmentPoints can be specified to batch events into 1 object. |
| 25 | * Each should be treated as independent events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 27 | * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 28 | * FIXME: Current implementation directly use this object as |
| 29 | * Replication message, but should be sending update operation info. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 30 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 31 | @JsonSerialize(using = HostEventSerializer.class) |
| 32 | public class HostEvent extends TopologyElement<HostEvent> { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 33 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 34 | private final MACAddress mac; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 35 | private List<SwitchPort> attachmentPoints; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 36 | private long lastSeenTime; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 39 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 40 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 41 | @Deprecated |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 42 | protected HostEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | mac = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame^] | 46 | /** |
| 47 | * Creates the Host object. |
| 48 | * |
| 49 | * @param mac the MAC address to identify the host |
| 50 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 51 | public HostEvent(MACAddress mac) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame^] | 52 | this.mac = checkNotNull(mac); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | this.attachmentPoints = new LinkedList<>(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 56 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 57 | * Creates an unfrozen copy of given Object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 58 | * |
| 59 | * @param original to make copy of. |
| 60 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 61 | public HostEvent(HostEvent original) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 62 | super(original); |
| 63 | this.mac = original.mac; |
| 64 | this.attachmentPoints = new ArrayList<>(original.attachmentPoints); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 65 | this.lastSeenTime = original.lastSeenTime; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 69 | public MACAddress getMac() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | return mac; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public List<SwitchPort> getAttachmentPoints() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 74 | return Collections.unmodifiableList(attachmentPoints); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public void setAttachmentPoints(List<SwitchPort> attachmentPoints) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 78 | if (isFrozen()) { |
| 79 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 80 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | this.attachmentPoints = attachmentPoints; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 84 | public void addAttachmentPoint(SwitchPort attachmentPoint) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 85 | if (isFrozen()) { |
| 86 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 87 | } |
| 88 | // may need to maintain uniqueness |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | this.attachmentPoints.add(0, attachmentPoint); |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 92 | 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 HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public String toString() { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 113 | return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 116 | @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 HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 132 | if (obj == null) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (getClass() != obj.getClass()) { |
| 137 | return false; |
| 138 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 139 | |
| 140 | if (!super.equals(obj)) { |
| 141 | return false; |
| 142 | } |
| 143 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 144 | HostEvent other = (HostEvent) obj; |
| 145 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 146 | // 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 HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 151 | // Assuming mac is unique cluster-wide |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 152 | public static ByteBuffer getHostID(final byte[] mac) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 153 | return (ByteBuffer) ByteBuffer.allocate(2 + mac.length) |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 154 | .putChar('H').put(mac).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 157 | public byte[] getID() { |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 158 | return getHostID(mac.toBytes()).array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public ByteBuffer getIDasByteBuffer() { |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 162 | return getHostID(mac.toBytes()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 163 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 164 | } |