blob: 0fa5a7e9dd1fda057d0156f5e7c8c77c6381c42f [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07003import net.onrc.onos.core.topology.web.serializers.PortEventSerializer;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07004import net.onrc.onos.core.util.Dpid;
5import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07006import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07007
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07008import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07009import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070010
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080011import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070012import java.util.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080013
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070015 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070016 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070017 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070018 * FIXME: Current implementation directly use this object as
19 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080020 */
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070021@JsonSerialize(using = PortEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070022public class PortEvent extends TopologyElement<PortEvent> {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070023
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024 private final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080025 // TODO Add Hardware Address
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070026
27 // TODO: Where should the attribute names be defined?
28 /**
29 * Attribute name for description.
30 */
31 public static final String DESCRIPTION = "description";
32
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080033
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080035 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080037 @Deprecated
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070038 protected PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080040 }
41
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070042 /**
43 * Creates the port object.
44 *
45 * @param switchPort SwitchPort to identify this port
46 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070047 public PortEvent(SwitchPort switchPort) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070048 this.id = checkNotNull(switchPort);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080049 }
50
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070051 /**
52 * Creates the port object.
53 *
54 * @param dpid SwitchPort to identify this port
55 * @param number PortNumber to identify this port
56 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070057 public PortEvent(Dpid dpid, PortNumber number) {
58 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080059 }
60
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070062 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 *
64 * @param original to make copy of.
65 */
66 public PortEvent(PortEvent original) {
67 super(original);
68 this.id = original.id;
69 }
70
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070071 /**
72 * Gets the SwitchPort identifying this port.
73 *
74 * @return SwitchPort
75 */
76 public SwitchPort getSwitchPort() {
77 return id;
78 }
79
80 /**
81 * Gets the Dpid of the switch this port belongs to.
82 *
83 * @return DPID
84 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070085 public Dpid getDpid() {
86 return id.getDpid();
87 }
88
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070089 /**
90 * Gets the port number.
91 *
92 * @return port number
93 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070094 public PortNumber getPortNumber() {
95 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080096 }
97
98 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070099 public boolean equals(Object o) {
100 if (this == o) {
101 return true;
102 }
103
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700104 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700105 return false;
106 }
107
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700108 if (getClass() != o.getClass()) {
109 return false;
110 }
111 PortEvent other = (PortEvent) o;
112
113 // compare attributes
114 if (!super.equals(o)) {
115 return false;
116 }
117
118 return Objects.equals(this.id, other.id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700119 }
120
121 @Override
122 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700123 return 31 * super.hashCode() + Objects.hashCode(id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700124 }
125
126 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800127 public String toString() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700128 return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800129 }
130
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800131 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
132
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700133 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700134 checkNotNull(dpid);
135 checkNotNull(number);
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700136 return getPortID(dpid.value(), number.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700137 }
138
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800139 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 if (dpid == null) {
141 throw new IllegalArgumentException("dpid cannot be null");
142 }
143 if (number == null) {
144 throw new IllegalArgumentException("number cannot be null");
145 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700146 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES)
147 .putChar('S').putLong(dpid)
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800149 }
150
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800151 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700152 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800153 }
154
155 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700156 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800157 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800158}