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;
13 import java.util.ArrayList;
14 import java.util.List;
15 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.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.class, 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.class, 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.class, 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("40 03 00 00 "
87 + "00 01 AA BB CC DD " // IPv4
88 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
91 @Test(expected = LispSerializationException.class)
92 public void deserialize__ShorterBuffer2() throws Exception {
93 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
98 public void serialize__Simple() throws Exception {
99 List<Hop> hops = new ArrayList<>();
100 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"))
101 .setHopId("hubba").build());
102 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
103 .setHopId("bubba").build());
105 ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
108 RlocBuilder rb = new RlocBuilder();
109 rb.setAddressType(ExplicitLocatorPathLcaf.class);
110 rb.setVirtualNetworkId(null);
111 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
112 .lisp.address.address.ExplicitLocatorPathBuilder()
113 .setExplicitLocatorPath(elpb.build()).build());
115 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
116 LispAddressSerializer.getInstance().serialize(buf, rb.build());
117 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
119 + "00 00 00 01 AA BB CC DD " // IPv4
120 + "00 00 00 01 11 22 33 44"); // IPv4
121 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
125 public void serialize__NoAddresses() throws Exception {
126 RlocBuilder rb = new RlocBuilder();
127 rb.setAddressType(ExplicitLocatorPathLcaf.class);
128 rb.setVirtualNetworkId(null);
129 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
130 .lisp.address.address.ExplicitLocatorPathBuilder().build());
132 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
133 LispAddressSerializer.getInstance().serialize(buf, rb.build());
134 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
136 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());