blob: e9256af24e48390068cc9d63f02360c818d7e5ef [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
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070072 /**
73 * Gets the SwitchPort identifying this port.
74 *
75 * @return SwitchPort
76 */
77 public SwitchPort getSwitchPort() {
78 return id;
79 }
80
81 /**
82 * Gets the Dpid of the switch this port belongs to.
83 *
84 * @return DPID
85 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070086 public Dpid getDpid() {
87 return id.getDpid();
88 }
89
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 /**
91 * Gets the port number.
92 *
93 * @return port number
94 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070095 public PortNumber getPortNumber() {
96 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 }
98
99 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700100 public boolean equals(Object o) {
101 if (this == o) {
102 return true;
103 }
104
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700105 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700106 return false;
107 }
108
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700109 if (getClass() != o.getClass()) {
110 return false;
111 }
112 PortEvent other = (PortEvent) o;
113
114 // compare attributes
115 if (!super.equals(o)) {
116 return false;
117 }
118
119 return Objects.equals(this.id, other.id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700120 }
121
122 @Override
123 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700124 return 31 * super.hashCode() + Objects.hashCode(id);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700125 }
126
127 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800128 public String toString() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700129 return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800130 }
131
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800132 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
133
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700134 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
135 Validate.notNull(dpid);
136 Validate.notNull(number);
137 return getPortID(dpid.value(), (long) number.value());
138 }
139
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800140 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 if (dpid == null) {
142 throw new IllegalArgumentException("dpid cannot be null");
143 }
144 if (number == null) {
145 throw new IllegalArgumentException("number cannot be null");
146 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700147 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES)
148 .putChar('S').putLong(dpid)
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800150 }
151
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800152 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700153 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800154 }
155
156 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700157 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800158 }
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700159
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800160}