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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import java.nio.ByteBuffer;
14 import java.util.ArrayList;
15 import java.util.List;
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.tools.junit.BaseTestCase;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
29 public class ExplicitLocatorPathSerializerTest extends BaseTestCase {
32 public void deserialize__Simple() throws Exception {
33 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
35 + "00 00 00 01 AA BB CC DD " // IPv4
36 + "00 00 00 01 11 22 33 44")); // IPv4
38 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
39 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
41 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
42 assertEquals(2, hops.size());
44 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
45 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
49 public void deserialize__Bits() throws Exception {
50 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
52 + "00 05 00 01 AA BB CC DD " // IPv4
53 + "00 02 00 01 11 22 33 44")); // IPv4
55 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
56 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
58 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
59 assertEquals(2, hops.size());
61 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
62 assertEquals(true, hops.get(0).getLrsBits().getLookup().booleanValue());
63 assertEquals(false, hops.get(0).getLrsBits().getRlocProbe().booleanValue());
64 assertEquals(true, hops.get(0).getLrsBits().getStrict().booleanValue());
65 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
66 assertEquals(false, hops.get(1).getLrsBits().getLookup().booleanValue());
67 assertEquals(true, hops.get(1).getLrsBits().getRlocProbe().booleanValue());
68 assertEquals(false, hops.get(1).getLrsBits().getStrict().booleanValue());
72 public void deserialize__NoAddresses() throws Exception {
73 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
76 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
77 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
79 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
83 @Test(expected = LispSerializationException.class)
84 public void deserialize__ShorterBuffer() throws Exception {
85 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
89 00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
92 @Test(expected = LispSerializationException.class)
93 public void deserialize__ShorterBuffer2() throws Exception {
94 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
99 public void serialize__Simple() throws Exception {
100 List<Hop> hops = new ArrayList<>();
101 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"))
102 .setHopId("hubba").build());
103 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
104 .setHopId("bubba").build());
106 ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
109 RlocBuilder rb = new RlocBuilder();
110 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
111 rb.setVirtualNetworkId(null);
112 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
113 .lisp.address.address.ExplicitLocatorPathBuilder()
114 .setExplicitLocatorPath(elpb.build()).build());
116 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
117 LispAddressSerializer.getInstance().serialize(buf, rb.build());
118 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
120 + "00 00 00 01 AA BB CC DD " // IPv4
121 + "00 00 00 01 11 22 33 44"); // IPv4
122 assertArrayEquals(expectedBuf.array(), buf.array());
126 public void serialize__NoAddresses() throws Exception {
127 RlocBuilder rb = new RlocBuilder();
128 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
129 rb.setVirtualNetworkId(null);
130 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
131 .lisp.address.address.ExplicitLocatorPathBuilder().build());
133 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
134 LispAddressSerializer.getInstance().serialize(buf, rb.build());
135 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
137 assertArrayEquals(expectedBuf.array(), buf.array());