factories and tests
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java
index a43e5d6..2e7a59b 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java
@@ -1,5 +1,7 @@
package org.projectfloodlight.openflow.types;
+import javax.annotation.Nonnull;
+
import org.projectfloodlight.openflow.annotations.Immutable;
import org.projectfloodlight.openflow.util.HexString;
@@ -38,13 +40,13 @@
}
/**
- * Creates a {@link DatapathId} from an {@link MacAddress}. This factory
+ * Creates a {@link DatapathId} from a {@link MacAddress}. This factory
* method assumes that the {@link DatapathId} is composed of two zero bytes
* appended by the {@link MacAddress}'s 6 bytes.
* @param mac the {@link MacAddress} to create the {@link DatapathId} from
* @return a {@link DatapathId} derived from the supplied {@link MacAddress}
*/
- public static DatapathId of(MacAddress mac) {
+ public static DatapathId of(@Nonnull MacAddress mac) {
byte[] zeroBytes = new byte[]{0,0};
byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes());
return DatapathId.of(fullBytes);
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/MacAddress.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/MacAddress.java
index 89debf0..b026b6c 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/MacAddress.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/MacAddress.java
@@ -8,6 +8,8 @@
import org.projectfloodlight.openflow.exceptions.OFParseError;
import org.projectfloodlight.openflow.util.HexString;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import com.google.common.hash.PrimitiveSink;
import com.google.common.primitives.Longs;
@@ -33,6 +35,10 @@
private static final long LLDP_MAC_ADDRESS_MASK = 0xfffffffffff0L;
private static final long LLDP_MAC_ADDRESS_VALUE = 0x0180c2000000L;
+ private static final String FORMAT_ERROR = "Mac address is not well-formed. " +
+ "It must consist of 6 hex digit pairs separated by colons: ";
+ private static final int STRING_LENGTH = 6 * 2 + 5;
+
private MacAddress(final long rawValue) {
this.rawValue = rawValue;
}
@@ -66,17 +72,14 @@
*/
@Nonnull
public static MacAddress of(@Nonnull final String macString) throws IllegalArgumentException {
- if (macString == null) {
- throw new NullPointerException("macString must not be null");
- }
+ Preconditions.checkArgument(!Strings.isNullOrEmpty(macString),
+ "macString must not be null or empty");
+ Preconditions.checkArgument(macString.length() == (STRING_LENGTH),
+ FORMAT_ERROR + macString);
+
int index = 0;
int shift = 40;
- final String FORMAT_ERROR = "Mac address is not well-formed. " +
- "It must consist of 6 hex digit pairs separated by colons: ";
-
long raw = 0;
- if (macString.length() != 6 * 2 + 5)
- throw new IllegalArgumentException(FORMAT_ERROR + macString);
while (shift >= 0) {
int digit1 = Character.digit(macString.charAt(index++), 16);
@@ -94,6 +97,21 @@
return MacAddress.of(raw);
}
+ /**
+ * Creates a {@link MacAddress} from a {@link DatapathId}. This factory
+ * method assumes that the {@link MacAddress} is composed of the last six
+ * bytes of the supplied {@link DatapathId}.
+ * @param dpid the {@link DatapathId} to create the {@link MacAddress} from
+ * @return a {@link MacAddress} derived from the supplied {@link DatapathId}
+ */
+ public static MacAddress of(@Nonnull DatapathId dpid) {
+ Preconditions.checkNotNull(dpid, "dpid must not be null");
+
+ byte[] dpidBytes = dpid.getBytes();
+ byte[] macBytes = Arrays.copyOfRange(dpidBytes, 2, 8);
+ return MacAddress.of(macBytes);
+ }
+
private volatile byte[] bytesCache = null;
public byte[] getBytes() {
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/DatapathIdTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/DatapathIdTest.java
new file mode 100644
index 0000000..136908b
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/DatapathIdTest.java
@@ -0,0 +1,70 @@
+package org.projectfloodlight.openflow.types;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class DatapathIdTest {
+
+ byte[][] testDpids = new byte[][] {
+ {0x0, 0x0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
+ {0x0, 0x0, (byte) 0x80, 0x0, 0x0, 0x0, 0x0, 0x01},
+ {0x0, 0x0, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255 }
+ };
+
+ String[] testStrings = {
+ "00:00:01:02:03:04:05:06",
+ "00:00:80:00:00:00:00:01",
+ "00:00:ff:ff:ff:ff:ff:ff"
+ };
+
+ long[] testInts = {
+ 0x000000010203040506L,
+ 0x000000800000000001L,
+ 0x000000ffffffffffffL
+ };
+
+ @Test
+ public void testOfString() {
+ for(int i=0; i < testDpids.length; i++ ) {
+ DatapathId dpid = DatapathId.of(testStrings[i]);
+ assertEquals(testInts[i], dpid.getLong());
+ assertArrayEquals(testDpids[i], dpid.getBytes());
+ assertEquals(testStrings[i], dpid.toString());
+ }
+ }
+
+ @Test
+ public void testOfByteArray() {
+ for(int i=0; i < testDpids.length; i++ ) {
+ DatapathId dpid = DatapathId.of(testDpids[i]);
+ assertEquals("error checking long representation of "+Arrays.toString(testDpids[i]) + "(should be "+Long.toHexString(testInts[i]) +")", testInts[i], dpid.getLong());
+ assertArrayEquals(testDpids[i], dpid.getBytes());
+ assertEquals(testStrings[i], dpid.toString());
+ }
+ }
+
+ @Test
+ public void testOfMacAddress() {
+
+ for (String s : testStrings) {
+
+ // Generate mac addresses by stripping off the front two bytes
+ MacAddress mac = MacAddress.of(s.replaceFirst("00:00:", ""));
+
+ // Create dpid from mac address
+ DatapathId candidateDpid = DatapathId.of(mac);
+
+ // Create dpid from string
+ DatapathId actualDpid = DatapathId.of(s);
+
+ assertThat(candidateDpid.equals(actualDpid), is(true));
+ }
+ }
+}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/MacAddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/MacAddressTest.java
index a13fdd4..e1c7721 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/MacAddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/MacAddressTest.java
@@ -1,17 +1,20 @@
package org.projectfloodlight.openflow.types;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
import java.util.Arrays;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Test;
import org.projectfloodlight.openflow.exceptions.OFParseError;
+import static org.hamcrest.Matchers.is;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
public class MacAddressTest {
byte[][] testAddresses = new byte[][] {
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
@@ -151,4 +154,16 @@
assertFalse(MacAddress.of("00:80:C2:f0:00:00").isLLDPAddress());
assertFalse(MacAddress.of("FE:80:C2:00:00:00").isLLDPAddress());
}
+
+ @Test
+ public void testOfDatapathid() {
+ MacAddress mac = MacAddress.of(DatapathId.NONE);
+ assertThat(mac, is(MacAddress.NONE));
+
+ for (String s : testStrings) {
+ DatapathId dpid = DatapathId.of("00:00:" + s);
+ mac = MacAddress.of(dpid);
+ assertThat(mac, is(MacAddress.of(s)));
+ }
+ }
}