Migrate isFoo() callers
[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().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.class, 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("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"))
101                 .setHopId("hubba").build());
102         hops.add(new HopBuilder().setAddress(SimpleAddressBuilder.getDefaultInstance("17.34.51.68"))
103                 .setHopId("bubba").build());
104
105         ExplicitLocatorPathBuilder elpb = new ExplicitLocatorPathBuilder();
106         elpb.setHop(hops);
107
108         RlocBuilder rb = new RlocBuilder();
109         rb.setAddressType(ExplicitLocatorPathLcaf.class);
110         rb.setVirtualNetworkId(null);
111         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
112             .lisp.address.address.ExplicitLocatorPathBuilder()
113             .setExplicitLocatorPath(elpb.build()).build());
114
115         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
116         LispAddressSerializer.getInstance().serialize(buf, rb.build());
117         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
118                 + "0A 00 00 10 "
119                 + "00 00 00 01 AA BB CC DD "  // IPv4
120                 + "00 00 00 01 11 22 33 44"); // IPv4
121         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
122     }
123
124     @Test
125     public void serialize__NoAddresses() throws Exception {
126         RlocBuilder rb = new RlocBuilder();
127         rb.setAddressType(ExplicitLocatorPathLcaf.class);
128         rb.setVirtualNetworkId(null);
129         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
130             .lisp.address.address.ExplicitLocatorPathBuilder().build());
131
132         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
133         LispAddressSerializer.getInstance().serialize(buf, rb.build());
134         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
135                 + "0A 00 00 00");
136         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
137     }
138 }