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;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.lispflowmapping.TestUtils.hexToByteBuffer;
17 import java.nio.ByteBuffer;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
22 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
27 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;
28 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;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
32 public class ExplicitLocatorPathSerializerTest {
35 public void deserialize__Simple() throws Exception {
36 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
38 + "00 00 00 01 AA BB CC DD " // IPv4
39 + "00 00 00 01 11 22 33 44")); // IPv4
41 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
42 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
44 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
45 assertEquals(2, hops.size());
47 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
48 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
52 public void deserialize__Bits() throws Exception {
53 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
55 + "00 05 00 01 AA BB CC DD " // IPv4
56 + "00 02 00 01 11 22 33 44")); // IPv4
58 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
59 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
61 List<Hop> hops = elp.getExplicitLocatorPath().nonnullHop();
62 assertEquals(2, hops.size());
64 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
65 assertTrue(hops.get(0).getLrsBits().getLookup());
66 assertFalse(hops.get(0).getLrsBits().getRlocProbe());
67 assertTrue(hops.get(0).getLrsBits().getStrict());
68 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
69 assertFalse(hops.get(1).getLrsBits().getLookup());
70 assertTrue(hops.get(1).getLrsBits().getRlocProbe());
71 assertFalse(hops.get(1).getLrsBits().getStrict());
75 public void deserialize__NoAddresses() throws Exception {
76 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
79 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
80 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
82 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
86 @Test(expected = LispSerializationException.class)
87 public void deserialize__ShorterBuffer() throws Exception {
88 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
92 00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
95 @Test(expected = LispSerializationException.class)
96 public void deserialize__ShorterBuffer2() throws Exception {
97 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
102 public void serialize__Simple() throws Exception {
103 List<Hop> hops = new ArrayList<>();
104 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"))
105 .setHopId("hubba").build());
106 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
107 .setHopId("bubba").build());
109 ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
112 RlocBuilder rb = new RlocBuilder();
113 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
114 rb.setVirtualNetworkId(null);
115 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
116 .lisp.address.address.ExplicitLocatorPathBuilder()
117 .setExplicitLocatorPath(elpb.build()).build());
119 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
120 LispAddressSerializer.getInstance().serialize(buf, rb.build());
121 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
123 + "00 00 00 01 AA BB CC DD " // IPv4
124 + "00 00 00 01 11 22 33 44"); // IPv4
125 assertArrayEquals(expectedBuf.array(), buf.array());
129 public void serialize__NoAddresses() throws Exception {
130 RlocBuilder rb = new RlocBuilder();
131 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
132 rb.setVirtualNetworkId(null);
133 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
134 .lisp.address.address.ExplicitLocatorPathBuilder().build());
136 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
137 LispAddressSerializer.getInstance().serialize(buf, rb.build());
138 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
140 assertArrayEquals(expectedBuf.array(), buf.array());