Adding port, port number, port description implementations and related tests.
diff --git a/net/api/src/main/java/org/onlab/onos/net/DeviceId.java b/net/api/src/main/java/org/onlab/onos/net/DeviceId.java
index 6f4cff2..19577a1 100644
--- a/net/api/src/main/java/org/onlab/onos/net/DeviceId.java
+++ b/net/api/src/main/java/org/onlab/onos/net/DeviceId.java
@@ -5,16 +5,29 @@
/**
* Immutable representation of a device identity.
*/
-public class DeviceId extends ElementId {
+public final class DeviceId extends ElementId {
- // TODO: Discuss whether we should just use ElementId for Device and Host alike
+ // Public construction is prohibited
+ private DeviceId(URI uri) {
+ super(uri);
+ }
+
/**
* Creates a device id using the supplied URI.
*
- * @param uri backing device URI
+ * @param uri device URI
*/
- public DeviceId(URI uri) {
- super(uri);
+ public static DeviceId deviceId(URI uri) {
+ return new DeviceId(uri);
+ }
+
+ /**
+ * Creates a device id using the supplied URI string.
+ *
+ * @param string device URI string
+ */
+ public static DeviceId deviceId(String string) {
+ return new DeviceId(URI.create(string));
}
}
diff --git a/net/api/src/main/java/org/onlab/onos/net/ElementId.java b/net/api/src/main/java/org/onlab/onos/net/ElementId.java
index e0f3add..9f7dffb 100644
--- a/net/api/src/main/java/org/onlab/onos/net/ElementId.java
+++ b/net/api/src/main/java/org/onlab/onos/net/ElementId.java
@@ -8,7 +8,7 @@
/**
* Immutable representation of a network element identity.
*/
-public class ElementId {
+public abstract class ElementId {
private final URI uri;
@@ -17,7 +17,7 @@
*
* @param uri backing URI
*/
- public ElementId(URI uri) {
+ protected ElementId(URI uri) {
this.uri = uri;
}
@@ -37,14 +37,12 @@
@Override
public boolean equals(Object obj) {
- if (this == obj) {
- return true;
+ if (obj instanceof ElementId) {
+ final ElementId that = (ElementId) obj;
+ return this.getClass() == that.getClass() &&
+ Objects.equals(this.uri, that.uri);
}
- if (obj == null || getClass() != obj.getClass()) {
- return false;
- }
- final ElementId other = (ElementId) obj;
- return Objects.equals(this.uri, other.uri);
+ return false;
}
@Override
diff --git a/net/api/src/main/java/org/onlab/onos/net/Port.java b/net/api/src/main/java/org/onlab/onos/net/Port.java
index 3135b77..24fd533 100644
--- a/net/api/src/main/java/org/onlab/onos/net/Port.java
+++ b/net/api/src/main/java/org/onlab/onos/net/Port.java
@@ -1,11 +1,18 @@
package org.onlab.onos.net;
+import java.util.Set;
+
/**
* Abstraction of a network port.
*/
public interface Port {
- // Notion of port state: enabled, disabled, blocked
+ /**
+ * Port state.
+ */
+ enum State {
+ UP, DOWN, BLOCKED, UNKNOWN
+ }
/**
* Returns the port number.
@@ -15,6 +22,27 @@
PortNumber number();
/**
+ * Returns the port state(s).
+ *
+ * @return port state set
+ */
+ Set<State> state();
+
+ /**
+ * Indicates whether or not the port is currently up and active.
+ *
+ * @return true if the port is operational
+ */
+ boolean isEnabled();
+
+ /**
+ * Indicates whether or not the port is administratively blocked.
+ *
+ * @return true if the port is blocked
+ */
+ boolean isBlocked();
+
+ /**
* Returns the identifier of the network element to which this port belongs.
*
* @return parent network element
diff --git a/net/api/src/main/java/org/onlab/onos/net/PortNumber.java b/net/api/src/main/java/org/onlab/onos/net/PortNumber.java
index 4ea4e24..45ee9ee 100644
--- a/net/api/src/main/java/org/onlab/onos/net/PortNumber.java
+++ b/net/api/src/main/java/org/onlab/onos/net/PortNumber.java
@@ -1,7 +1,73 @@
package org.onlab.onos.net;
+import com.google.common.primitives.UnsignedLongs;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
/**
* Representation of a port number.
*/
-public interface PortNumber {
+public final class PortNumber {
+
+ private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
+
+ private final long number;
+
+ // Public creation is prohibited
+ private PortNumber(long number) {
+ checkArgument(number >= 0 && number < MAX_NUMBER,
+ "Port number %d is outside the supported range [0, %d)",
+ number, MAX_NUMBER);
+ this.number = number;
+ }
+
+ /**
+ * Returns the port number representing the specified long value.
+ *
+ * @param number port number as long value
+ * @return port number
+ */
+ public static PortNumber portNumber(long number) {
+ return new PortNumber(number);
+ }
+
+ /**
+ * Returns the port number representing the specified string value.
+ *
+ * @param string port number as string value
+ * @return port number
+ */
+ public static PortNumber portNumber(String string) {
+ return new PortNumber(UnsignedLongs.decode(string));
+ }
+
+ /**
+ * Returns the backing long value.
+ *
+ * @return port number as long
+ */
+ public long toLong() {
+ return number;
+ }
+
+ @Override
+ public String toString() {
+ return UnsignedLongs.toString(number);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(number);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof PortNumber) {
+ final PortNumber other = (PortNumber) obj;
+ return this.number == other.number;
+ }
+ return false;
+ }
}
diff --git a/net/api/src/main/java/org/onlab/onos/net/device/DefaultPortDescription.java b/net/api/src/main/java/org/onlab/onos/net/device/DefaultPortDescription.java
index c171098..c434ff5 100644
--- a/net/api/src/main/java/org/onlab/onos/net/device/DefaultPortDescription.java
+++ b/net/api/src/main/java/org/onlab/onos/net/device/DefaultPortDescription.java
@@ -1,7 +1,32 @@
package org.onlab.onos.net.device;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.onos.net.Port;
+import org.onlab.onos.net.PortNumber;
+
+import java.util.Set;
+
/**
* Default implementation of immutable port description.
*/
public class DefaultPortDescription implements PortDescription {
+
+ private final PortNumber number;
+ private final Set<Port.State> state;
+
+ public DefaultPortDescription(PortNumber number, Set<Port.State> state) {
+ this.number = number;
+ this.state = ImmutableSet.copyOf(state);
+ }
+
+ @Override
+ public PortNumber portNumber() {
+ return number;
+ }
+
+ @Override
+ public Set<Port.State> portState() {
+ return state;
+ }
+
}
diff --git a/net/api/src/main/java/org/onlab/onos/net/device/PortDescription.java b/net/api/src/main/java/org/onlab/onos/net/device/PortDescription.java
index 4c944ab..2f86248 100644
--- a/net/api/src/main/java/org/onlab/onos/net/device/PortDescription.java
+++ b/net/api/src/main/java/org/onlab/onos/net/device/PortDescription.java
@@ -1,5 +1,10 @@
package org.onlab.onos.net.device;
+import org.onlab.onos.net.Port;
+import org.onlab.onos.net.PortNumber;
+
+import java.util.Set;
+
/**
* Information about a port.
*/
@@ -7,4 +12,18 @@
// TODO: possibly relocate this to a common ground so that this can also used by host tracking if required
+ /**
+ * Returns the port number.
+ *
+ * @return port number
+ */
+ PortNumber portNumber();
+
+ /**
+ * Returns the port state set.
+ *
+ * @return set of port states
+ */
+ Set<Port.State> portState();
+
}