Add LinkTuple
- Object to identify a link between ports.
-- Switch -> Dpid, Port -> SwitchPort, Link -> LinkTuple
Implicit assumption here is that even if we allow multiple Links departing from a Port,
their destination will be different ports.
Change-Id: Iad3fb068e5dab0a1b1bea63f257bce2a8e773fb3
diff --git a/src/main/java/net/onrc/onos/core/util/LinkTuple.java b/src/main/java/net/onrc/onos/core/util/LinkTuple.java
new file mode 100644
index 0000000..4e80826
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/util/LinkTuple.java
@@ -0,0 +1,99 @@
+package net.onrc.onos.core.util;
+
+import java.util.Objects;
+
+import org.apache.commons.lang.Validate;
+
+
+/**
+ * Immutable class to identify a Link between 2 ports.
+ */
+public final class LinkTuple {
+
+ private final SwitchPort src;
+ private final SwitchPort dst;
+
+ /**
+ * Default constructor for Serializer.
+ */
+ protected LinkTuple() {
+ src = null;
+ dst = null;
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param src source port
+ * @param dst destination port
+ */
+ public LinkTuple(SwitchPort src, SwitchPort dst) {
+ Validate.notNull(src);
+ Validate.notNull(dst);
+
+ this.src = src;
+ this.dst = dst;
+ }
+
+ /**
+ * Creates the Link object.
+ *
+ * @param srcDpid source switch DPID
+ * @param srcPortNo source port number
+ * @param dstDpid destination switch DPID
+ * @param dstPortNo destination port number
+ */
+ public LinkTuple(Dpid srcDpid, PortNumber srcPortNo,
+ Dpid dstDpid, PortNumber dstPortNo) {
+ this(new SwitchPort(srcDpid, srcPortNo),
+ new SwitchPort(dstDpid, dstPortNo));
+ }
+
+ /**
+ * Gets the source port.
+ *
+ * @return source port
+ */
+ public SwitchPort getSrc() {
+ return src;
+ }
+
+ /**
+ * Gets the destination port.
+ *
+ * @return destination port
+ */
+ public SwitchPort getDst() {
+ return dst;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((src == null) ? 0 : src.hashCode());
+ result = prime * result + ((dst == null) ? 0 : dst.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ LinkTuple other = (LinkTuple) obj;
+ return Objects.equals(src, other.src) &&
+ Objects.equals(dst, other.dst);
+ }
+
+ @Override
+ public String toString() {
+ return "(" + src + "=>" + dst + ")";
+ }
+}
diff --git a/src/main/java/net/onrc/onos/core/util/SwitchPort.java b/src/main/java/net/onrc/onos/core/util/SwitchPort.java
index b9e1964..cbe4bcb 100644
--- a/src/main/java/net/onrc/onos/core/util/SwitchPort.java
+++ b/src/main/java/net/onrc/onos/core/util/SwitchPort.java
@@ -2,6 +2,7 @@
import net.onrc.onos.core.util.serializers.SwitchPortSerializer;
+import org.apache.commons.lang.Validate;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
@@ -28,6 +29,8 @@
* @param port the port to use.
*/
public SwitchPort(Dpid dpid, PortNumber port) {
+ Validate.notNull(dpid);
+ Validate.notNull(port);
this.dpid = dpid;
this.port = port;
}