PortNumber also backed by port name
Change-Id: Id47781be714bf1efe7d008c8e97ab172177a85f9
diff --git a/core/api/src/main/java/org/onosproject/net/PortNumber.java b/core/api/src/main/java/org/onosproject/net/PortNumber.java
index 16a4193..db19807 100644
--- a/core/api/src/main/java/org/onosproject/net/PortNumber.java
+++ b/core/api/src/main/java/org/onosproject/net/PortNumber.java
@@ -15,10 +15,10 @@
*/
package org.onosproject.net;
-import java.util.Objects;
-
import com.google.common.primitives.UnsignedLongs;
+import java.util.Objects;
+
/**
* Representation of a port number.
*/
@@ -48,10 +48,17 @@
public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER);
private final long number;
+ private final String name;
// Public creation is prohibited
private PortNumber(long number) {
this.number = number;
+ this.name = UnsignedLongs.toString(number);
+ }
+
+ private PortNumber(long number, String name) {
+ this.number = number;
+ this.name = name;
}
/**
@@ -75,6 +82,17 @@
}
/**
+ * Returns the port number representing the specified long value and name.
+ *
+ * @param number port number as long value
+ * @param name port name as string value
+ * @return port number
+ */
+ public static PortNumber portNumber(long number, String name) {
+ return new PortNumber(number, name);
+ }
+
+ /**
* Indicates whether or not this port number is a reserved logical one or
* whether it corresponds to a normal physical port of a device or NIC.
*
@@ -115,7 +133,7 @@
@Override
public String toString() {
if (!isLogical()) {
- return UnsignedLongs.toString(number);
+ return name;
} else {
return decodeLogicalPort();
}
diff --git a/core/api/src/test/java/org/onosproject/net/PortNumberTest.java b/core/api/src/test/java/org/onosproject/net/PortNumberTest.java
index 142f516..9f2b76a 100644
--- a/core/api/src/test/java/org/onosproject/net/PortNumberTest.java
+++ b/core/api/src/test/java/org/onosproject/net/PortNumberTest.java
@@ -15,12 +15,11 @@
*/
package org.onosproject.net;
-import static org.junit.Assert.assertEquals;
-import static org.onosproject.net.PortNumber.portNumber;
-
+import com.google.common.testing.EqualsTester;
import org.junit.Test;
-import com.google.common.testing.EqualsTester;
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.net.PortNumber.portNumber;
/**
* Test of the port number.
@@ -30,9 +29,8 @@
@Test
public void basics() {
new EqualsTester()
- .addEqualityGroup(portNumber(123),
- portNumber("123"))
- .addEqualityGroup(portNumber(321))
+ .addEqualityGroup(portNumber(123), portNumber("123"))
+ .addEqualityGroup(portNumber(321), portNumber(321, "LIM-3-2-1"))
.testEquals();
}