blob: 86785cda8e01548108d8948322939947d496adf8 [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 HIGUCHI93d35ea2014-08-31 23:26:13 -07003import net.onrc.onos.core.topology.web.serializers.PortDataSerializer;
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/**
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070015 * Self-contained Port object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070017@JsonSerialize(using = PortDataSerializer.class)
18public class PortData extends TopologyElement<PortData> {
19 public static final int PORTID_BYTES = SwitchData.SWITCHID_BYTES + 2 + 8;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070020
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070021 private final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022 // TODO Add Hardware Address
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023
24 // TODO: Where should the attribute names be defined?
25 /**
26 * Attribute name for description.
27 */
28 public static final String DESCRIPTION = "description";
29
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080030 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080031 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080032 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080033 @Deprecated
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070034 protected PortData() {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036 }
37
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070038 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070039 * Constructor for given SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070040 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070041 * @param switchPort the SwitchPort to identify the port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070042 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070043 public PortData(SwitchPort switchPort) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070044 this.id = checkNotNull(switchPort);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080045 }
46
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070047 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070048 * Constructor for given switch DPID and port number.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070049 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070050 * @param dpid the DPID of the switch the port belongs to
51 * @param number the PortNumber to identify the port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070052 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070053 public PortData(Dpid dpid, PortNumber number) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070054 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080055 }
56
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070058 * Copy constructor.
59 * <p>
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070060 * Creates an unfrozen copy of the given PortData object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070062 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070064 public PortData(PortData original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 super(original);
66 this.id = original.id;
67 }
68
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 /**
70 * Gets the SwitchPort identifying this port.
71 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070072 * @return the SwitchPort identifying this port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070073 */
74 public SwitchPort getSwitchPort() {
75 return id;
76 }
77
78 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070079 * Gets the DPID of the switch this port belongs to.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070080 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070081 * @return the DPID of the switch this port belongs to
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070082 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070083 public Dpid getDpid() {
84 return id.getDpid();
85 }
86
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070087 /**
88 * Gets the port number.
89 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070090 * @return the port number
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070091 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070092 public PortNumber getPortNumber() {
93 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080094 }
95
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070096 /**
97 * Computes the port ID for a given switch DPID and a port number.
98 *
99 * @param dpid the switch DPID to use
100 * @param number the port number to use
101 * @return the port ID as a ByteBuffer
102 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700103 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700104 checkNotNull(dpid);
105 checkNotNull(number);
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700106 return getPortID(dpid.value(), number.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700107 }
108
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700109 /**
110 * Computes the port ID for a given switch DPID and a port number.
111 * <p>
112 * TODO: This method should be removed and replaced with the corresponding
113 * getPortID(Dpid, PortNumber) method.
114 *
115 * @param dpid the switch DPID to use
116 * @param number the port number to use
117 * @return the port ID as a ByteBuffer
118 */
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800119 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 if (dpid == null) {
121 throw new IllegalArgumentException("dpid cannot be null");
122 }
123 if (number == null) {
124 throw new IllegalArgumentException("number cannot be null");
125 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700126 return (ByteBuffer) ByteBuffer.allocate(PortData.PORTID_BYTES)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700127 .putChar('S').putLong(dpid)
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800129 }
130
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700131 @Override
132 public Dpid getOriginDpid() {
133 return this.id.getDpid();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800134 }
135
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700136 @Override
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800137 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700138 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800139 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700140
141 @Override
142 public int hashCode() {
143 return 31 * super.hashCode() + Objects.hashCode(id);
144 }
145
146 @Override
147 public boolean equals(Object o) {
148 if (this == o) {
149 return true;
150 }
151
152 if (o == null || getClass() != o.getClass()) {
153 return false;
154 }
155
156 // Compare attributes
157 if (!super.equals(o)) {
158 return false;
159 }
160
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700161 PortData other = (PortData) o;
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700162 return Objects.equals(this.id, other.id);
163 }
164
165 @Override
166 public String toString() {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700167 return "[PortData " + getDpid() + "@" + getPortNumber() + "]";
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700168 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800169}