Merge "Use odlparent-lite as artifacts parent"
[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
16 import junitx.framework.ArrayAssert;
17
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;
31
32 public class ExplicitLocatorPathSerializerTest extends BaseTestCase {
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.class, 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", String.valueOf(hops.get(0).getAddress().getValue()));
48         assertEquals("17.34.51.68", String.valueOf(hops.get(1).getAddress().getValue()));
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.class, address.getAddressType());
59         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
60
61         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
62         assertEquals(2, hops.size());
63
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());
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.class, address.getAddressType());
80         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
81
82         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
83         assertEquals(0, hops.size());
84     }
85
86     @Test(expected = LispSerializationException.class)
87     public void deserialize__ShorterBuffer() throws Exception {
88         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
89                 "0A 00 00 18 " + //
90                 "00 01 AA BB CC DD " + // IPv4
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<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());
105
106         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
107         elpb.setHop(hops);
108
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());
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.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());
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 }