blob: 161842575cd075908d7986876d60897a98aba2df [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.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08005
Ray Milkey2fa6ca42014-06-13 15:38:20 -07006import net.onrc.onos.core.topology.web.serializers.LinkEventSerializer;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07007import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -07008import net.onrc.onos.core.util.LinkTuple;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07009import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070010import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070011
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070012import org.apache.commons.lang.Validate;
Ray Milkey2fa6ca42014-06-13 15:38:20 -070013import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
15/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070016 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070017 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070018 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070019 * FIXME: Current implementation directly use this object as
20 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080021 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070022
23@JsonSerialize(using = LinkEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024public class LinkEvent extends TopologyElement<LinkEvent> {
25
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070026 private final LinkTuple id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 // TODO add LastSeenTime, Capacity if appropriate
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080028
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080029 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080030 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080032 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070033 protected LinkEvent() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070034 id = null;
35 }
36
37 /**
38 * Creates the Link object.
39 *
40 * @param id link tuple to identify this link
41 */
42 public LinkEvent(LinkTuple id) {
43 Validate.notNull(id);
44
45 this.id = id;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080046 }
47
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070048 /**
49 * Creates the Link object.
50 *
51 * @param src source SwitchPort
52 * @param dst destination SwitchPort
53 */
54 public LinkEvent(SwitchPort src, SwitchPort dst) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070055 this(new LinkTuple(src, dst));
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 }
57
58 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070059 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070060 *
61 * @param original to make copy of.
62 */
63 public LinkEvent(LinkEvent original) {
64 super(original);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070065 Validate.isTrue(Objects.equals(this.id, original.id));
66
67 this.id = original.id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070068 }
69
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070070 // TODO probably want to remove this
Toshio Koide0c9106d2014-02-19 15:26:38 -080071 public LinkEvent(Link link) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070072 this(link.getLinkTuple());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070073 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080074 }
75
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070076 /**
77 * Creates the Link object.
78 *
79 * @param srcDpid source switch DPID
80 * @param srcPortNo source port number
81 * @param dstDpid destination switch DPID
82 * @param dstPortNo destination port number
83 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070084 public LinkEvent(Dpid srcDpid, PortNumber srcPortNo,
85 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070086 this(new LinkTuple(srcDpid, srcPortNo, dstDpid, dstPortNo));
87 }
88
89 /**
90 * Gets a {@link LinkTuple} that identifies this link.
91 *
92 * @return a LinkTuple representing the Port
93 */
94 public LinkTuple getLinkTuple() {
95 return id;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070096 }
97
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070098 /**
99 * Gets the source SwitchPort.
100 *
101 * @return source SwitchPort.
102 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800103 public SwitchPort getSrc() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700104 return getLinkTuple().getSrc();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800105 }
106
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700107 /**
108 * Gets the destination SwitchPort.
109 *
110 * @return destination SwitchPort.
111 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800112 public SwitchPort getDst() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700113 return getLinkTuple().getDst();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800114 }
115
116 @Override
117 public String toString() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700118 return "[LinkEvent " + getSrc() + "->" + getDst() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800119 }
120
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800121 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
122
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700123 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
124 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700125 return getLinkID(srcDpid.value(), (long) srcPortNo.value(),
126 dstDpid.value(), (long) dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700127 }
128
Ray Milkey9526d6f2014-04-10 14:54:15 -0700129 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
130 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700131 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
132 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700133 .put(PortEvent.getPortID(srcDpid, srcPortNo))
134 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800135 }
136
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800137 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700138 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800139 }
140
141 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700142 return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(),
143 getDst().getDpid(), getDst().getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800144 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800145
Toshio Koide0c9106d2014-02-19 15:26:38 -0800146 @Override
147 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 final int prime = 31;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700149 int result = super.hashCode();
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700150 result = prime * result + Objects.hashCode(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800152 }
153
154 @Override
155 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700156 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700158 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700159
Ray Milkeyb29e6262014-04-09 16:02:14 -0700160 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700161 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700162 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700163
Ray Milkeyb29e6262014-04-09 16:02:14 -0700164 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700165 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700166 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 LinkEvent other = (LinkEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700168
169 // compare attributes
170 if (!super.equals(obj)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700171 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700172 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700173
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700174 return Objects.equals(this.id, other.id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800175 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700176}