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