Bump versions by x.(y+1).z
[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.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.lispflowmapping.TestUtils.hexToByteBuffer;
16
17 import java.nio.ByteBuffer;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
22 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
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;
31
32 public class ExplicitLocatorPathSerializerTest {
33
34     @Test
35     public void deserialize__Simple() throws Exception {
36         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
37                 + "0A 00 00 10 "
38                 + "00 00 00 01 AA BB CC DD "   // IPv4
39                 + "00 00 00 01 11 22 33 44")); // IPv4
40
41         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
42         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
43
44         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
45         assertEquals(2, hops.size());
46
47         assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
48         assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
49     }
50
51     @Test
52     public void deserialize__Bits() throws Exception {
53         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
54                 + "0A 00 00 10 "
55                 + "00 05 00 01 AA BB CC DD "   // IPv4
56                 + "00 02 00 01 11 22 33 44")); // IPv4
57
58         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
59         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
60
61         List<Hop> hops = elp.getExplicitLocatorPath().nonnullHop();
62         assertEquals(2, hops.size());
63
64         assertEquals("170.187.204.221", hops.get(0).getAddress().stringValue());
65         assertTrue(hops.get(0).getLrsBits().getLookup());
66         assertFalse(hops.get(0).getLrsBits().getRlocProbe());
67         assertTrue(hops.get(0).getLrsBits().getStrict());
68         assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
69         assertFalse(hops.get(1).getLrsBits().getLookup());
70         assertTrue(hops.get(1).getLrsBits().getRlocProbe());
71         assertFalse(hops.get(1).getLrsBits().getStrict());
72     }
73
74     @Test
75     public void deserialize__NoAddresses() throws Exception {
76         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
77                 + "0A 00 00 00 "));
78
79         assertEquals(ExplicitLocatorPathLcaf.VALUE, address.getAddressType());
80         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
81
82         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
83         assertNull(hops);
84     }
85
86     @Test(expected = LispSerializationException.class)
87     public void deserialize__ShorterBuffer() throws Exception {
88         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
89             40 03 00 00 \
90             0A 00 00 18 \
91             00 01 AA BB CC DD \
92             00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
93     }
94
95     @Test(expected = LispSerializationException.class)
96     public void deserialize__ShorterBuffer2() throws Exception {
97         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
98                 + "0A 00 00 18 "));
99     }
100
101     @Test
102     public void serialize__Simple() throws Exception {
103         List<Hop> hops = new ArrayList<>();
104         hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"))
105                 .setHopId("hubba").build());
106         hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
107                 .setHopId("bubba").build());
108
109         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
110         elpb.setHop(hops);
111
112         RlocBuilder rb = new RlocBuilder();
113         rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
114         rb.setVirtualNetworkId(null);
115         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
116             .lisp.address.address.ExplicitLocatorPathBuilder()
117             .setExplicitLocatorPath(elpb.build()).build());
118
119         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
120         LispAddressSerializer.getInstance().serialize(buf, rb.build());
121         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
122                 + "0A 00 00 10 "
123                 + "00 00 00 01 AA BB CC DD "  // IPv4
124                 + "00 00 00 01 11 22 33 44"); // IPv4
125         assertArrayEquals(expectedBuf.array(), buf.array());
126     }
127
128     @Test
129     public void serialize__NoAddresses() throws Exception {
130         RlocBuilder rb = new RlocBuilder();
131         rb.setAddressType(ExplicitLocatorPathLcaf.VALUE);
132         rb.setVirtualNetworkId(null);
133         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
134             .lisp.address.address.ExplicitLocatorPathBuilder().build());
135
136         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
137         LispAddressSerializer.getInstance().serialize(buf, rb.build());
138         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
139                 + "0A 00 00 00");
140         assertArrayEquals(expectedBuf.array(), buf.array());
141     }
142 }