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;
16 import junitx.framework.ArrayAssert;
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;
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 extends BaseTestCase {
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.class, 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", String.valueOf(hops.get(0).getAddress().getValue()));
48 assertEquals("17.34.51.68", String.valueOf(hops.get(1).getAddress().getValue()));
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.class, address.getAddressType());
59 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
61 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
62 assertEquals(2, hops.size());
64 assertEquals("170.187.204.221", String.valueOf(hops.get(0).getAddress().getValue()));
65 assertEquals(true, hops.get(0).getLrsBits().isLookup().booleanValue());
66 assertEquals(false, hops.get(0).getLrsBits().isRlocProbe().booleanValue());
67 assertEquals(true, hops.get(0).getLrsBits().isStrict().booleanValue());
68 assertEquals("17.34.51.68", String.valueOf(hops.get(1).getAddress().getValue()));
69 assertEquals(false, hops.get(1).getLrsBits().isLookup().booleanValue());
70 assertEquals(true, hops.get(1).getLrsBits().isRlocProbe().booleanValue());
71 assertEquals(false, hops.get(1).getLrsBits().isStrict().booleanValue());
75 public void deserialize__NoAddresses() throws Exception {
76 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
79 assertEquals(ExplicitLocatorPathLcaf.class, address.getAddressType());
80 ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
82 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
83 assertEquals(0, hops.size());
86 @Test(expected = LispSerializationException.class)
87 public void deserialize__ShorterBuffer() throws Exception {
88 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
90 "00 01 AA BB CC DD " + // IPv4
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<Hop>();
103 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221")).build());
104 hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68")).build());
106 ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
109 RlocBuilder rb = new RlocBuilder();
110 rb.setAddressType(ExplicitLocatorPathLcaf.class);
111 rb.setVirtualNetworkId(null);
112 rb.setAddress((Address)
113 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.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 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
126 public void serialize__NoAddresses() throws Exception {
127 RlocBuilder rb = new RlocBuilder();
128 rb.setAddressType(ExplicitLocatorPathLcaf.class);
129 rb.setVirtualNetworkId(null);
130 rb.setAddress((Address)
131 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.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 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());