Implement packet seriailizer and deserializer for LACP
Change-Id: Idbbd87a3ddeb477cac49a65e6a5c768761019e11
diff --git a/utils/misc/src/test/java/org/onlab/packet/lacp/LacpBaseTlvTest.java b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpBaseTlvTest.java
new file mode 100644
index 0000000..874d5c1
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpBaseTlvTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onlab.packet.lacp;
+
+import com.google.common.io.Resources;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.MacAddress;
+
+import static org.junit.Assert.*;
+
+public class LacpBaseTlvTest {
+ private static final String PACKET_DUMP = "baseinfo.bin";
+
+ private byte[] data;
+
+ private static final short SYS_PRIORITY = (short) 32768;
+ private static final MacAddress SYS_MAC = MacAddress.valueOf("a4:23:05:00:11:22");
+ private static final short KEY = (short) 13;
+ private static final short PORT_PRIORITY = (short) 32768;
+ private static final short PORT = 22;
+ private static final byte STATE = (byte) 0x85;
+
+ static final LacpBaseTlv BASE_TLV = new LacpBaseTlv()
+ .setSystemPriority(SYS_PRIORITY)
+ .setSystemMac(SYS_MAC)
+ .setKey(KEY)
+ .setPortPriority(PORT_PRIORITY)
+ .setPort(PORT)
+ .setState(STATE);
+
+ @Before
+ public void setUp() throws Exception {
+ data = Resources.toByteArray(LacpBaseTlvTest.class.getResource(PACKET_DUMP));
+ }
+
+ @Test
+ public void deserializer() throws Exception {
+ LacpBaseTlv actorInfo = LacpBaseTlv.deserializer().deserialize(data, 0, data.length);
+ assertEquals(SYS_PRIORITY, actorInfo.getSystemPriority());
+ assertEquals(SYS_MAC, actorInfo.getSystemMac());
+ assertEquals(KEY, actorInfo.getKey());
+ assertEquals(PORT_PRIORITY, actorInfo.getPortPriority());
+ assertEquals(PORT, actorInfo.getPort());
+ assertEquals(STATE, actorInfo.getState().toByte());
+ }
+
+ @Test
+ public void serialize() {
+ assertArrayEquals(data, BASE_TLV.serialize());
+ }
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/packet/lacp/LacpCollectorTlvTest.java b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpCollectorTlvTest.java
new file mode 100644
index 0000000..1e098ee
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpCollectorTlvTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onlab.packet.lacp;
+
+import com.google.common.io.Resources;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class LacpCollectorTlvTest {
+ private static final String PACKET_DUMP = "collectorinfo.bin";
+
+ private byte[] data;
+
+ private static final short COLLECTOR_MAX_DELAY = (short) 32768;
+
+ static final LacpCollectorTlv COLLECTOR_TLV = new LacpCollectorTlv()
+ .setCollectorMaxDelay(COLLECTOR_MAX_DELAY);
+
+ @Before
+ public void setUp() throws Exception {
+ data = Resources.toByteArray(LacpCollectorTlvTest.class.getResource(PACKET_DUMP));
+ }
+
+ @Test
+ public void deserializer() throws Exception {
+ LacpCollectorTlv lacpCollectorTlv = LacpCollectorTlv.deserializer().deserialize(data, 0, data.length);
+ assertEquals(COLLECTOR_MAX_DELAY, lacpCollectorTlv.getCollectorMaxDelay());
+
+ }
+
+ @Test
+ public void serialize() {
+ assertArrayEquals(data, COLLECTOR_TLV.serialize());
+ }
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/packet/lacp/LacpStateTest.java b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpStateTest.java
new file mode 100644
index 0000000..4645664
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpStateTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.lacp;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class LacpStateTest {
+
+ @Test
+ public void toByte() {
+ LacpState state = new LacpState((byte) 0x15);
+ assertEquals((byte) 0x15, state.toByte());
+ }
+
+ @Test
+ public void isActive() {
+ LacpState state = new LacpState((byte) 0x1);
+ assertTrue(state.isActive());
+ }
+
+ @Test
+ public void isTimeout() {
+ LacpState state = new LacpState((byte) 0x2);
+ assertTrue(state.isTimeout());
+ }
+
+ @Test
+ public void isAggregatable() {
+ LacpState state = new LacpState((byte) 0x4);
+ assertTrue(state.isAggregatable());
+ }
+
+ @Test
+ public void isSync() {
+ LacpState state = new LacpState((byte) 0x8);
+ assertTrue(state.isSync());
+ }
+
+ @Test
+ public void isCollecting() {
+ LacpState state = new LacpState((byte) 0x10);
+ assertTrue(state.isCollecting());
+ }
+
+ @Test
+ public void isDistributing() {
+ LacpState state = new LacpState((byte) 0x20);
+ assertTrue(state.isDistributing());
+ }
+
+ @Test
+ public void isDefault() {
+ LacpState state = new LacpState((byte) 0x40);
+ assertTrue(state.isDefault());
+ }
+
+ @Test
+ public void isExpired() {
+ LacpState state = new LacpState((byte) 0x80);
+ assertTrue(state.isExpired());
+ }
+
+ @Test
+ public void equals() {
+ LacpState state1 = new LacpState((byte) 0x15);
+ LacpState state2 = new LacpState((byte) 0x15);
+ LacpState state3 = new LacpState((byte) 0x51);
+
+ assertEquals(state1, state2);
+ assertNotEquals(state1, state3);
+
+ }
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTerminatorTlvTest.java b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTerminatorTlvTest.java
new file mode 100644
index 0000000..6f84a5a
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTerminatorTlvTest.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onlab.packet.lacp;
+
+import com.google.common.io.Resources;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+
+public class LacpTerminatorTlvTest {
+ private static final String PACKET_DUMP = "terminatorinfo.bin";
+
+ private byte[] data;
+
+ static final LacpTerminatorTlv TERMINATOR_TLV = new LacpTerminatorTlv();
+
+ @Before
+ public void setUp() throws Exception {
+ data = Resources.toByteArray(LacpTerminatorTlvTest.class.getResource(PACKET_DUMP));
+ }
+
+ @Test
+ public void deserializer() throws Exception {
+ LacpTerminatorTlv lacpTerminatorTlv = LacpTerminatorTlv.deserializer().deserialize(data, 0, data.length);
+ }
+
+ @Test
+ public void serialize() {
+ assertArrayEquals(data, TERMINATOR_TLV.serialize());
+ }
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTest.java b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTest.java
new file mode 100644
index 0000000..810799c
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/lacp/LacpTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.lacp;
+
+import com.google.common.collect.Maps;
+import com.google.common.io.Resources;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+public class LacpTest {
+ private static final String PACKET_DUMP = "lacp.bin";
+
+ private static final byte LACP_VERSION = 1;
+ private static final Map<Byte, LacpTlv> LACP_TLV = Maps.newHashMap();
+ private static final Lacp LACP = new Lacp();
+
+ static {
+ LACP_TLV.put(Lacp.TYPE_ACTOR, LacpBaseTlvTest.BASE_TLV);
+ LACP_TLV.put(Lacp.TYPE_PARTNER, LacpBaseTlvTest.BASE_TLV);
+ LACP_TLV.put(Lacp.TYPE_COLLECTOR, LacpCollectorTlvTest.COLLECTOR_TLV);
+ LACP_TLV.put(Lacp.TYPE_TERMINATOR, LacpTerminatorTlvTest.TERMINATOR_TLV);
+
+ LACP.setLacpVersion(LACP_VERSION)
+ .setTlv(LACP_TLV);
+ }
+
+ private byte[] data;
+
+ @Before
+ public void setUp() throws Exception {
+ data = Resources.toByteArray(LacpBaseTlvTest.class.getResource(PACKET_DUMP));
+ }
+
+ @Test
+ public void deserializer() throws Exception {
+ Lacp lacp = Lacp.deserializer().deserialize(data, 0, data.length);
+ assertEquals(LACP_VERSION, lacp.getLacpVersion());
+ assertEquals(LACP_TLV, lacp.getTlv());
+ }
+
+ @Test
+ public void serialize() {
+ assertArrayEquals(data, LACP.serialize());
+ }
+}
\ No newline at end of file