First implementation of BNG application

The application offers network level APIs for the BNG data plane.

An attachment (BngAttachment) at network level is identified by an arbitrary string.
The exposed APIs are key-value like APIs.

Change-Id: If0e484f487ea16dd8c7dd99642f75686e1dbc29a
diff --git a/app/src/test/java/org/opencord/bng/packets/GenericPppTest.java b/app/src/test/java/org/opencord/bng/packets/GenericPppTest.java
new file mode 100644
index 0000000..42d0193
--- /dev/null
+++ b/app/src/test/java/org/opencord/bng/packets/GenericPppTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2019-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.opencord.bng.packets;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class GenericPppTest {
+    private Deserializer<GenericPpp> deserializer;
+
+    private byte code = 0x1;
+    private byte identifier = 0x1;
+    private short length = 0x08;
+    private byte[] payload = new byte[]{0x1, 0x4, 0x3, 0x4};
+    private byte[] padding = new byte[]{0x0, 0x0, 0x0};
+
+    private String packetToString = "";
+
+    private byte[] bytes;
+    private byte[] bytesPadded;
+
+    @Before
+    public void setUp() throws Exception {
+        deserializer = GenericPpp.deserializer();
+        ByteBuffer bb = ByteBuffer.allocate(Ppp.MIN_HEADER_LENGTH + 4);
+
+        bb.put(code);
+        bb.put(identifier);
+        bb.putShort(length);
+        bb.put(payload);
+
+        bytes = bb.array();
+
+        ByteBuffer bbPadded = ByteBuffer.allocate(Ppp.MIN_HEADER_LENGTH + 4 + 3);
+
+        bbPadded.put(code);
+        bbPadded.put(identifier);
+        bbPadded.putShort(length);
+        bbPadded.put(payload);
+        bbPadded.put(padding);
+
+        bytesPadded = bbPadded.array();
+    }
+
+    private short getTypeLength(byte type, short length) {
+        return (short) ((0x7f & type) << 9 | 0x1ff & length);
+    }
+
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+//        PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
+    }
+
+    /**
+     * Tests deserialize and getters.
+     */
+    @Test
+    public void testDeserialize() throws Exception {
+        GenericPpp ppp = deserializer.deserialize(bytes, 0, bytes.length);
+
+        assertEquals(code, ppp.getCode());
+        assertEquals(identifier, ppp.getIdentifier());
+        assertEquals(length, ppp.getLength());
+        assertArrayEquals(payload, ppp.getPayload().serialize());
+    }
+
+    /**
+     * Tests deserialize with padded packet.
+     */
+    @Test
+    public void testDeserializePadded() throws Exception {
+        GenericPpp ppp = deserializer.deserialize(bytesPadded, 0, bytesPadded.length);
+
+        assertEquals(code, ppp.getCode());
+        assertEquals(identifier, ppp.getIdentifier());
+        assertEquals(length, ppp.getLength());
+        assertArrayEquals(payload, ppp.getPayload().serialize());
+    }
+
+}
diff --git a/app/src/test/java/org/opencord/bng/packets/IpcpTest.java b/app/src/test/java/org/opencord/bng/packets/IpcpTest.java
new file mode 100644
index 0000000..eeaa341
--- /dev/null
+++ b/app/src/test/java/org/opencord/bng/packets/IpcpTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2019-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.opencord.bng.packets;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class IpcpTest {
+
+    private Deserializer<Ipcp> deserializer;
+
+    private byte ipAddressTlvSize = 0x6;
+    private IpAddress ipAddress = IpAddress.valueOf("10.0.0.1");
+
+    private byte optionalTlvSize = 0x6;
+    private byte optionalTlvType = 0x01;
+    private byte[] optionalTlvValue = new byte[]{0x4, 0x3, 0x2, 0x1};
+
+    private byte code = Ipcp.CONF_REQ;
+    private byte identifier = 0x1;
+    private short length = (short) (Ipcp.MIN_HEADER_LENGTH + ipAddressTlvSize + optionalTlvSize);
+
+
+    private byte[] bytes;
+
+    @Before
+    public void setUp() throws Exception {
+        deserializer = Ipcp.deserializer();
+        ByteBuffer bb = ByteBuffer.allocate(Ipcp.MIN_HEADER_LENGTH + ipAddressTlvSize + optionalTlvSize);
+
+        bb.put(code);
+        bb.put(identifier);
+        bb.putShort(length);
+
+        bb.put(PppTlv.IPCPTLV_IP_ADDRESS);
+        bb.put(ipAddressTlvSize);
+        bb.put(ipAddress.toOctets());
+
+        bb.put(optionalTlvType);
+        bb.put(optionalTlvSize);
+        bb.put(optionalTlvValue);
+
+        bytes = bb.array();
+    }
+
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
+    }
+
+    /**
+     * Tests deserialize and getters.
+     */
+    @Test
+    public void testDeserialize() throws Exception {
+        Ipcp ipcp = deserializer.deserialize(bytes, 0, bytes.length);
+        PppTlv optionalTlv = ipcp.getIpcpTlvList().get(0);
+
+        assertEquals(code, ipcp.getCode());
+        assertEquals(identifier, ipcp.getIdentifier());
+        assertEquals(length, ipcp.getLength());
+
+        assertEquals(optionalTlvType, optionalTlv.getType());
+        assertEquals(optionalTlvSize, optionalTlv.getLength());
+        assertArrayEquals(optionalTlvValue, optionalTlv.getValue());
+
+        assertEquals(PppTlv.IPCPTLV_IP_ADDRESS, ipcp.getIpAddressTlv().getType());
+        assertEquals(ipAddressTlvSize, ipcp.getIpAddressTlv().getLength());
+        assertEquals(ipAddress, IpAddress.valueOf(IpAddress.Version.INET,
+                                                  ipcp.getIpAddressTlv().getValue()));
+        assertEquals(ipAddress, ipcp.getIpAddress());
+    }
+}
diff --git a/app/src/test/java/org/opencord/bng/packets/PppoeTest.java b/app/src/test/java/org/opencord/bng/packets/PppoeTest.java
new file mode 100644
index 0000000..aa1e24a
--- /dev/null
+++ b/app/src/test/java/org/opencord/bng/packets/PppoeTest.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2019-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.opencord.bng.packets;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+public class PppoeTest {
+    private Deserializer<Pppoe> deserializer;
+
+    private byte versionAndType = 0x11;
+    private byte code = 0x9;
+    private byte codeSession = 0x0;
+    private short sessionId = 0x0102;
+    private short payloadLength = 0;
+
+    private byte[] padding = new byte[]{0x0, 0x0, 0x0};
+
+    private byte[] bytesDisc;
+    private byte[] bytesPadded;
+    private byte[] bytesSession;
+
+    @Before
+    public void setUp() throws Exception {
+        deserializer = Pppoe.deserializer();
+        ByteBuffer bb = ByteBuffer.allocate(Pppoe.HEADER_LENGTH);
+
+        bb.put(versionAndType);
+        bb.put(code);
+        bb.putShort(sessionId);
+        bb.putShort(payloadLength);
+
+        bytesDisc = bb.array();
+        ByteBuffer bbPadded = ByteBuffer.allocate(Pppoe.HEADER_LENGTH + 3);
+
+        bbPadded.put(versionAndType);
+        bbPadded.put(code);
+        bbPadded.putShort(sessionId);
+        bbPadded.putShort(payloadLength);
+        bbPadded.put(padding);
+
+        bytesPadded = bbPadded.array();
+
+        ByteBuffer bbSession = ByteBuffer.allocate(Pppoe.HEADER_LENGTH + 2);
+
+        bbSession.put(versionAndType);
+        bbSession.put(codeSession);
+        bbSession.putShort(sessionId);
+        bbSession.putShort((short) (payloadLength + 2));
+        bbSession.putShort(PppProtocolType.LCP.code());
+
+        bytesSession = bbSession.array();
+    }
+
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, bytesDisc);
+    }
+
+    /**
+     * Tests deserialize and getters.
+     */
+    @Test
+    public void testDeserializeDiscovery() throws Exception {
+        Pppoe pppoe = deserializer.deserialize(bytesDisc, 0, bytesDisc.length);
+
+        assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
+        assertEquals(code, pppoe.getPacketType().code());
+        assertEquals(sessionId, pppoe.getSessionId());
+        assertEquals(payloadLength, pppoe.getPayloadLength());
+    }
+
+    /**
+     * Tests deserialize with padded packet.
+     */
+    @Test
+    public void testDeserializePadded() throws Exception {
+        Pppoe pppoe = deserializer.deserialize(bytesPadded, 0, bytesPadded.length);
+
+        assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
+        assertEquals(code, pppoe.getPacketType().code());
+        assertEquals(sessionId, pppoe.getSessionId());
+        assertEquals(payloadLength, pppoe.getPayloadLength());
+    }
+
+    /**
+     * Tests deserialize of PPPoE session packets.
+     */
+    @Test
+    public void testDeserializeSession() throws Exception {
+        Pppoe pppoe = deserializer.deserialize(bytesSession, 0, bytesSession.length);
+
+        assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
+        assertEquals(codeSession, pppoe.getPacketType().code());
+        assertEquals(sessionId, pppoe.getSessionId());
+        assertEquals(payloadLength + 2, pppoe.getPayloadLength());
+        assertEquals(PppProtocolType.LCP.code(), pppoe.getPppProtocol());
+    }
+}