2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.serializer.address;
10 import static org.junit.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
18 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
19 import org.opendaylight.lispflowmapping.lisp.type.AddressFamilyNumberEnum;
20 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
21 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
22 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
23 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LcafApplicationDataAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lcafapplicationdataaddress.AddressBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.lispaddresscontainer.address.lcafapplicationdata.LcafApplicationDataAddrBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
30 public class LispApplicationDataLCAFAddressTest extends BaseTestCase {
33 public void deserialize__Simple() throws Exception {
34 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
36 "AA BB CC DD " + // IPTOS & protocol
37 "A6 A1 A6 A2 " + // local port range
38 "FF DD FF DE " + // remote port range
39 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
41 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
42 LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) address;
44 assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), appAddress.getAddress().getPrimitiveAddress());
45 assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }), appAddress.getIpTos().intValue());
46 assertEquals((byte) 0xDD, appAddress.getProtocol().byteValue());
47 assertEquals((short) 0xA6A1, appAddress.getLocalPortLow().getValue().shortValue());
48 assertEquals((short) 0xA6A2, appAddress.getLocalPortHigh().getValue().shortValue());
49 assertEquals((short) 0xFFDD, appAddress.getRemotePortLow().getValue().shortValue());
50 assertEquals((short) 0xFFDE, appAddress.getRemotePortHigh().getValue().shortValue());
51 assertEquals(LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode(), appAddress.getLcafType().byteValue());
54 @Test(expected = LispSerializationException.class)
55 public void deserialize__ShorterBuffer() throws Exception {
56 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
61 @Test(expected = LispSerializationException.class)
62 public void deserialize__UnknownLCAFType() throws Exception {
63 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
64 "AA 20 00 12 " + // Type AA is unknown
65 "AA BB CC DD " + // IPTOS & protocol
66 "A6 A1 A6 A2 " + // local port range
67 "FF DD FF DE " + // remote port range
68 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
72 public void deserialize__Ipv6() throws Exception {
73 LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) LispAddressSerializer.getInstance().deserialize(
74 hexToByteBuffer("40 03 00 00 " + //
76 "AA BB CC DD " + // IPTOS & protocol
77 "A6 A1 A6 A2 " + // local port range
78 "FF DD FF DE " + // remote port range
79 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
82 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), appAddress
83 .getAddress().getPrimitiveAddress());
88 public void serialize__Simple() throws Exception {
89 LcafApplicationDataAddrBuilder addressBuilder = new LcafApplicationDataAddrBuilder();
90 addressBuilder.setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
91 addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
92 (short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
93 addressBuilder.setProtocol((short) 0xDD);
94 addressBuilder.setLocalPortLow(new PortNumber(0xA6A1));
95 addressBuilder.setLocalPortHigh(new PortNumber(0xA6A2));
96 addressBuilder.setRemotePortLow(new PortNumber(0xFFDD));
97 addressBuilder.setRemotePortHigh(new PortNumber(0xFFDE));
98 addressBuilder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
99 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
100 LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
101 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
103 "AA BB CC DD " + // IPTOS & protocol
104 "A6 A1 A6 A2 " + // local port range
105 "FF DD FF DE " + // remote port range
106 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
107 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());