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.address.LispAddressSerializerContext;
19 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
20 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
31 public class SourceDestKeySerializerTest extends BaseTestCase {
34 public void deserialize__Simple() throws Exception {
35 Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
37 "00 00 10 18 " + // reserved + masks
38 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
39 "00 01 22 33 44 55"), // AFI=1, IP=0x22334455
40 new LispAddressSerializerContext(null));
42 assertEquals(SourceDestKeyLcaf.class, address.getAddressType());
43 SourceDestKey srcDestAddress = (SourceDestKey) address.getAddress();
45 assertEquals((byte) 0x10, MaskUtil.getMaskForIpPrefix(srcDestAddress.getSourceDestKey().getSource()));
46 assertEquals((byte) 0x18, MaskUtil.getMaskForIpPrefix(srcDestAddress.getSourceDestKey().getDest()));
48 assertEquals("17.34.51.68/16", String.valueOf(srcDestAddress.getSourceDestKey().getSource().getValue()));
49 assertEquals("34.51.68.85/24", String.valueOf(srcDestAddress.getSourceDestKey().getDest().getValue()));
52 @Test(expected = LispSerializationException.class)
53 public void deserialize__ShorterBuffer() throws Exception {
54 LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
56 "AA BB "), new LispAddressSerializerContext(null));
59 @Test(expected = LispSerializationException.class)
60 public void deserialize__UnknownLCAFType() throws Exception {
61 LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
62 "AA 20 00 0A " + // Type AA is unknown
63 "00 00 CC DD " + // reserved + masks
64 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
65 "00 01 22 33 44 55"), // AFI=1, IP=0x22334455
66 new LispAddressSerializerContext(null));
70 public void deserialize__Ipv6() throws Exception {
71 Eid srcAddress = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
73 "00 00 78 78 " + // reserved + masks
74 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
75 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD"), // AFI=2,
76 new LispAddressSerializerContext(null));
79 assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd/120", String.valueOf(
80 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getSource().getValue()));
81 assertEquals("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd/120", String.valueOf(
82 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getDest().getValue()));
86 public void serialize__Simple() throws Exception {
87 SourceDestKeyBuilder addressBuilder = new SourceDestKeyBuilder();
88 addressBuilder.setSource(new IpPrefix(new Ipv4Prefix("17.34.51.68/8")));
89 addressBuilder.setDest(new IpPrefix(new Ipv4Prefix("34.51.68.85/16")));
91 EidBuilder eb = new EidBuilder();
92 eb.setAddressType(SourceDestKeyLcaf.class);
93 eb.setVirtualNetworkId(null);
94 eb.setAddress((Address)
95 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKeyBuilder()
96 .setSourceDestKey(addressBuilder.build()).build());
98 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
99 LispAddressSerializer.getInstance().serialize(buf, eb.build());
101 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
103 "00 00 08 10 " + // reserved + masks
104 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
105 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
106 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());