fc6f2cbb22683b135d637ca299e7c76af644ab6f
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / ExplicitLocatorPathSerializerTest.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.serializer.address;
9
10 import static org.junit.Assert.assertEquals;
11
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;
28
29 public class ExplicitLocatorPathSerializerTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
34                 + "0A 00 00 10 "
35                 + "00 00 00 01 AA BB CC DD "   // IPv4
36                 + "00 00 00 01 11 22 33 44")); // IPv4
37
38         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
39         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
40
41         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
42         assertEquals(2, hops.size());
43
44         assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
45         assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
46     }
47
48     @Test
49     public void deserialize__Bits() throws Exception {
50         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
51                 + "0A 00 00 10 "
52                 + "00 05 00 01 AA BB CC DD "   // IPv4
53                 + "00 02 00 01 11 22 33 44")); // IPv4
54
55         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
56         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
57
58         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
59         assertEquals(2, hops.size());
60
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());
69     }
70
71     @Test
72     public void deserialize__NoAddresses() throws Exception {
73         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
74                 + "0A 00 00 00 "));
75
76         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
77         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
78
79         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
80         assertNull(hops);
81     }
82
83     @Test(expected = LispSerializationException.class)
84     public void deserialize__ShorterBuffer() throws Exception {
85         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
86             40 03 00 00 \
87             0A 00 00 18 \
88             00 01 AA BB CC DD \
89             00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
90     }
91
92     @Test(expected = LispSerializationException.class)
93     public void deserialize__ShorterBuffer2() throws Exception {
94         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
95                 + "0A 00 00 18 "));
96     }
97
98     @Test
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());
105
106         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
107         elpb.setHop(hops);
108
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());
115
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 "
119                 + "0A 00 00 10 "
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());
123     }
124
125     @Test
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());
132
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 "
136                 + "0A 00 00 00");
137         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
138     }
139 }