blob: 222a6419a742d4e7144f7cec2a42d13485237f53 [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
8import org.apache.commons.lang.Validate;
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 HIGUCHIbf0a8712014-06-30 18:59:46 -070017 * TODO: Rename to match what it is. (Switch/Port/Link/Device)Snapshot?
18 * 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) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070048 Validate.notNull(switchPort);
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070049 this.id = switchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080050 }
51
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070052 /**
53 * Creates the port object.
54 *
55 * @param dpid SwitchPort to identify this port
56 * @param number PortNumber to identify this port
57 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070058 public PortEvent(Dpid dpid, PortNumber number) {
59 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080060 }
61
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070062 /**
63 * Create an unfrozen copy of given Object.
64 *
65 * @param original to make copy of.
66 */
67 public PortEvent(PortEvent original) {
68 super(original);
69 this.id = original.id;
70 }
71
72 // TODO remove me when ready
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070073 public PortEvent(Long dpid, Long number) {
74 this.id = new SwitchPort(dpid, number);
75 }
76
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070077 /**
78 * Gets the SwitchPort identifying this port.
79 *
80 * @return SwitchPort
81 */
82 public SwitchPort getSwitchPort() {
83 return id;
84 }
85
86 /**
87 * Gets the Dpid of the switch this port belongs to.
88 *
89 * @return DPID
90 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070091 public Dpid getDpid() {
92 return id.getDpid();
93 }
94
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070095 /**
96 * Gets the port number.
97 *
98 * @return port number
99 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700100 public PortNumber getPortNumber() {
101 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800102 }
103
104 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700105 public boolean equals(Object o) {
106 if (this == o) {
107 return true;
108 }
109
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700110 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700111 return false;
112 }
113
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700114 if (getClass() != o.getClass()) {
115 return false;
116 }
117 PortEvent other = (PortEvent) o;
118
119 // compare attributes
120 if (!super.equals(o)) {
121 return false;
122 }
123
124 return Objects.equals(this.id, other.id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700125 }
126
127 @Override
128 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700129 return 31 * super.hashCode() + Objects.hashCode(id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700130 }
131
132 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800133 public String toString() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700134 return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800135 }
136
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800137 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
138
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700139 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
140 Validate.notNull(dpid);
141 Validate.notNull(number);
142 return getPortID(dpid.value(), (long) number.value());
143 }
144
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800145 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 if (dpid == null) {
147 throw new IllegalArgumentException("dpid cannot be null");
148 }
149 if (number == null) {
150 throw new IllegalArgumentException("number cannot be null");
151 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700152 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES)
153 .putChar('S').putLong(dpid)
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800155 }
156
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800157 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700158 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800159 }
160
161 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700162 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800163 }
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700164
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800165}