Migrate to use stringValue()
[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.class, 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.class, 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().isLookup().booleanValue());
63         assertEquals(false, hops.get(0).getLrsBits().isRlocProbe().booleanValue());
64         assertEquals(true, hops.get(0).getLrsBits().isStrict().booleanValue());
65         assertEquals("17.34.51.68", hops.get(1).getAddress().stringValue());
66         assertEquals(false, hops.get(1).getLrsBits().isLookup().booleanValue());
67         assertEquals(true, hops.get(1).getLrsBits().isRlocProbe().booleanValue());
68         assertEquals(false, hops.get(1).getLrsBits().isStrict().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.class, address.getAddressType());
77         ExplicitLocatorPath elp = (ExplicitLocatorPath) address.getAddress();
78
79         List<Hop> hops = elp.getExplicitLocatorPath().getHop();
80         assertEquals(0, hops.size());
81     }
82
83     @Test(expected = LispSerializationException.class)
84     public void deserialize__ShorterBuffer() throws Exception {
85         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
86                 "0A 00 00 18 " + //
87                 "00 01 AA BB CC DD " + // IPv4
88                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
89     }
90
91     @Test(expected = LispSerializationException.class)
92     public void deserialize__ShorterBuffer2() throws Exception {
93         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
94                 "0A 00 00 18 "));
95     }
96
97     @Test
98     public void serialize__Simple() throws Exception {
99         List<Hop> hops = new ArrayList<>();
100         hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("170.187.204.221")).build());
101         hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68")).build());
102
103         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
104         elpb.setHop(hops);
105
106         RlocBuilder rb = new RlocBuilder();
107         rb.setAddressType(ExplicitLocatorPathLcaf.class);
108         rb.setVirtualNetworkId(null);
109         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
110             .lisp.address.address.ExplicitLocatorPathBuilder()
111             .setExplicitLocatorPath(elpb.build()).build());
112
113         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
114         LispAddressSerializer.getInstance().serialize(buf, rb.build());
115         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
116                 "0A 00 00 10 " + //
117                 "00 00 00 01 AA BB CC DD " + // IPv4
118                 "00 00 00 01 11 22 33 44"); // IPv4
119         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
120     }
121
122     @Test
123     public void serialize__NoAddresses() throws Exception {
124         RlocBuilder rb = new RlocBuilder();
125         rb.setAddressType(ExplicitLocatorPathLcaf.class);
126         rb.setVirtualNetworkId(null);
127         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
128             .lisp.address.address.ExplicitLocatorPathBuilder().build());
129
130         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
131         LispAddressSerializer.getInstance().serialize(buf, rb.build());
132         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
133                 "0A 00 00 00");
134         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
135     }
136 }