a9b1db0bbc657273eda48050933aedde0d39b66a
[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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
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;
30
31 public class ExplicitLocatorPathSerializerTest extends BaseTestCase {
32
33     @Test
34     public void deserialize__Simple() throws Exception {
35         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
36                 + "0A 00 00 10 "
37                 + "00 00 00 01 AA BB CC DD "   // IPv4
38                 + "00 00 00 01 11 22 33 44")); // IPv4
39
40         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
41         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
42
43         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
44         assertEquals(2, hops.size());
45
46         assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
47         assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
48     }
49
50     @Test
51     public void deserialize__Bits() throws Exception {
52         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
53                 + "0A 00 00 10 "
54                 + "00 05 00 01 AA BB CC DD "   // IPv4
55                 + "00 02 00 01 11 22 33 44")); // IPv4
56
57         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
58         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
59
60         List<Hop> hops = elp.getExplicitLocatorPath().nonnullHop();
61         assertEquals(2, hops.size());
62
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());
71     }
72
73     @Test
74     public void deserialize__NoAddresses() throws Exception {
75         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
76                 + "0A 00 00 00 "));
77
78         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
79         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
80
81         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
82         assertNull(hops);
83     }
84
85     @Test(expected = LispSerializationException.class)
86     public void deserialize__ShorterBuffer() throws Exception {
87         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
88             40 03 00 00 \
89             0A 00 00 18 \
90             00 01 AA BB CC DD \
91             00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
92     }
93
94     @Test(expected = LispSerializationException.class)
95     public void deserialize__ShorterBuffer2() throws Exception {
96         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
97                 + "0A 00 00 18 "));
98     }
99
100     @Test
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());
107
108         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
109         elpb.setHop(hops);
110
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());
117
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 "
121                 + "0A 00 00 10 "
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());
125     }
126
127     @Test
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());
134
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 "
138                 + "0A 00 00 00");
139         assertArrayEquals(expectedBuf.array(), buf.array());
140     }
141 }