Optimizing performance of HostId.
diff --git a/core/api/src/main/java/org/onlab/onos/net/DeviceId.java b/core/api/src/main/java/org/onlab/onos/net/DeviceId.java
index ef8c5ab..072ba28 100644
--- a/core/api/src/main/java/org/onlab/onos/net/DeviceId.java
+++ b/core/api/src/main/java/org/onlab/onos/net/DeviceId.java
@@ -1,15 +1,32 @@
package org.onlab.onos.net;
import java.net.URI;
+import java.util.Objects;
/**
* Immutable representation of a device identity.
*/
public final class DeviceId extends ElementId {
+ /**
+ * Represents either no device, or an unspecified device.
+ */
+ public static final DeviceId NONE = deviceId("none:none");
+
+ private final URI uri;
+ private final String str;
+
// Public construction is prohibited
private DeviceId(URI uri) {
- super(uri);
+ this.uri = uri;
+ this.str = uri.toString();
+ }
+
+
+ // Default constructor for serialization
+ protected DeviceId() {
+ this.uri = null;
+ this.str = null;
}
/**
@@ -30,4 +47,36 @@
return deviceId(URI.create(string));
}
+ /**
+ * Returns the backing URI.
+ *
+ * @return backing URI
+ */
+ public URI uri() {
+ return uri;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(str);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof DeviceId) {
+ final DeviceId that = (DeviceId) obj;
+ return this.getClass() == that.getClass() &&
+ Objects.equals(this.str, that.str);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return str;
+ }
+
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/ElementId.java b/core/api/src/main/java/org/onlab/onos/net/ElementId.java
index 3f9c88d..c179d00 100644
--- a/core/api/src/main/java/org/onlab/onos/net/ElementId.java
+++ b/core/api/src/main/java/org/onlab/onos/net/ElementId.java
@@ -1,62 +1,7 @@
package org.onlab.onos.net;
-import java.net.URI;
-import java.util.Objects;
-
/**
* Immutable representation of a network element identity.
*/
public abstract class ElementId {
-
- private final URI uri;
- private final String str;
-
- // Default constructor for serialization
- protected ElementId() {
- this.uri = null;
- this.str = null;
- }
-
- /**
- * Creates an element identifier using the supplied URI.
- *
- * @param uri backing URI
- */
- protected ElementId(URI uri) {
- this.uri = uri;
- this.str = uri.toString();
- }
-
- /**
- * Returns the backing URI.
- *
- * @return backing URI
- */
- public URI uri() {
- return uri;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(str);
- }
-
- @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.str, that.str);
- }
- return false;
- }
-
- @Override
- public String toString() {
- return str;
- }
-
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/HostId.java b/core/api/src/main/java/org/onlab/onos/net/HostId.java
index f2c0303..ffe558f 100644
--- a/core/api/src/main/java/org/onlab/onos/net/HostId.java
+++ b/core/api/src/main/java/org/onlab/onos/net/HostId.java
@@ -3,44 +3,69 @@
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
-import java.net.URI;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
/**
* Immutable representation of a host identity.
*/
public final class HostId extends ElementId {
- private static final String NIC = "nic";
-
/**
* Represents either no host, or an unspecified host; used for creating
* open ingress/egress edge links.
*/
- public static final HostId NONE = hostId(NIC + ":none-0");
+ public static final HostId NONE = new HostId(MacAddress.ZERO, VlanId.NONE);
+
+ private static final int MAC_LENGTH = 17;
+ private static final int MIN_ID_LENGTH = 19;
+
+ private final MacAddress mac;
+ private final VlanId vlanId;
// Public construction is prohibited
- private HostId(URI uri) {
- super(uri);
+ private HostId(MacAddress mac, VlanId vlanId) {
+ this.mac = mac;
+ this.vlanId = vlanId;
+ }
+
+ // Default constructor for serialization
+ private HostId() {
+ this.mac = null;
+ this.vlanId = null;
}
/**
- * Creates a device id using the supplied URI.
+ * Returns the host MAC address.
*
- * @param uri device URI
- * @return host identifier
+ * @return MAC address
*/
- public static HostId hostId(URI uri) {
- return new HostId(uri);
+ public MacAddress mac() {
+ return mac;
}
/**
- * Creates a device id using the supplied URI string.
+ * Returns the host MAC address.
+ *
+ * @return MAC address
+ */
+ public VlanId vlanId() {
+ return vlanId;
+ }
+
+ /**
+ * Creates a device id using the supplied ID string.
*
* @param string device URI string
* @return host identifier
*/
public static HostId hostId(String string) {
- return hostId(URI.create(string));
+ checkArgument(string.length() >= MIN_ID_LENGTH,
+ "Host ID must be at least %s characters", MIN_ID_LENGTH);
+ MacAddress mac = MacAddress.valueOf(string.substring(0, MAC_LENGTH));
+ VlanId vlanId = VlanId.vlanId(Short.parseShort(string.substring(MAC_LENGTH + 1)));
+ return new HostId(mac, vlanId);
}
/**
@@ -51,7 +76,7 @@
* @return host identifier
*/
public static HostId hostId(MacAddress mac, VlanId vlanId) {
- return hostId(NIC + ":" + mac + "-" + vlanId);
+ return new HostId(mac, vlanId);
}
/**
@@ -64,4 +89,26 @@
return hostId(mac, VlanId.vlanId(VlanId.UNTAGGED));
}
+ public String toString() {
+ return mac + "/" + vlanId;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mac, vlanId);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof HostId) {
+ final HostId other = (HostId) obj;
+ return Objects.equals(this.mac, other.mac) &&
+ Objects.equals(this.vlanId, other.vlanId);
+ }
+ return false;
+ }
+
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/HostLocation.java b/core/api/src/main/java/org/onlab/onos/net/HostLocation.java
index 60c5945..3fc3127 100644
--- a/core/api/src/main/java/org/onlab/onos/net/HostLocation.java
+++ b/core/api/src/main/java/org/onlab/onos/net/HostLocation.java
@@ -1,11 +1,18 @@
package org.onlab.onos.net;
+import static org.onlab.onos.net.PortNumber.P0;
+
/**
* Representation of a network edge location where an end-station host is
* connected.
*/
public class HostLocation extends ConnectPoint {
+ /**
+ * Represents a no location or an unknown location.
+ */
+ public static final HostLocation NONE = new HostLocation(DeviceId.NONE, P0, 0L);
+
// Note that time is explicitly excluded from the notion of equality.
private final long time;