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.assertTrue;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
20 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
26 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;
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.HopBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
31 public class ExplicitLocatorPathSerializerTest extends BaseTestCase {
34 public void deserialize__Simple() throws Exception {
35 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
37 + "00 00 00 01 AA BB CC DD " // IPv4
38 + "00 00 00 01 11 22 33 44")); // IPv4
40 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
41 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
43 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
44 assertEquals(2, hops.size());
46 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
47 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
51 public void deserialize__Bits() throws Exception {
52 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
54 + "00 05 00 01 AA BB CC DD " // IPv4
55 + "00 02 00 01 11 22 33 44")); // IPv4
57 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
58 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
60 List<Hop> hops = elp.getExplicitLocatorPath().nonnullHop();
61 assertEquals(2, hops.size());
63 assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
64 assertTrue(hops.get(0).getLrsBits().getLookup());
65 assertFalse(hops.get(0).getLrsBits().getRlocProbe());
66 assertTrue(hops.get(0).getLrsBits().getStrict());
67 assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
68 assertFalse(hops.get(1).getLrsBits().getLookup());
69 assertTrue(hops.get(1).getLrsBits().getRlocProbe());
70 assertFalse(hops.get(1).getLrsBits().getStrict());
74 public void deserialize__NoAddresses() throws Exception {
75 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
78 assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
79 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
81 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
85 @Test(expected = LispSerializationException.class)
86 public void deserialize__ShorterBuffer() throws Exception {
87 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
91 00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
94 @Test(expected = LispSerializationException.class)
95 public void deserialize__ShorterBuffer2() throws Exception {
96 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
101 public void serialize__Simple() throws Exception {
102 List<Hop> hops = new ArrayList<>();
103 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"))
104 .setHopId("hubba").build());
105 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
106 .setHopId("bubba").build());
108 ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
111 RlocBuilder rb = new RlocBuilder();
112 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
113 rb.setVirtualNetworkId(null);
114 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
115 .lisp.address.address.ExplicitLocatorPathBuilder()
116 .setExplicitLocatorPath(elpb.build()).build());
118 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
119 LispAddressSerializer.getInstance().serialize(buf, rb.build());
120 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
122 + "00 00 00 01 AA BB CC DD " // IPv4
123 + "00 00 00 01 11 22 33 44"); // IPv4
124 assertArrayEquals(expectedBuf.array(), buf.array());
128 public void serialize__NoAddresses() throws Exception {
129 RlocBuilder rb = new RlocBuilder();
130 rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
131 rb.setVirtualNetworkId(null);
132 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
133 .lisp.address.address.ExplicitLocatorPathBuilder().build());
135 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
136 LispAddressSerializer.getInstance().serialize(buf, rb.build());
137 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
139 assertArrayEquals(expectedBuf.array(), buf.array());