Bump MRI upstreams
[bgpcep.git] / bgp / extensions / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / esi / types / RouterIdParserTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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.protocol.bgp.evpn.impl.esi.types;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.LD;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VALUE_SIZE;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
16 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.LD_NID;
17 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.RD_NID;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.Esi;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.esi.ArbitraryCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.esi.RouterIdGeneratedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.esi.RouterIdGeneratedCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.esi.router.id.generated._case.RouterIdGenerated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.esi.esi.router.id.generated._case.RouterIdGeneratedBuilder;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35
36 public class RouterIdParserTest {
37     public static final byte[] RESULT = {(byte) 0x04, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02,
38         (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x00};
39     private static final byte[] VALUE = {(byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02, (byte) 0x02,
40         (byte) 0x02, (byte) 0x02, (byte) 0x00};
41     private static final Ipv4AddressNoZone ROUTE_ID = new Ipv4AddressNoZone("42.42.42.42");
42     public static final RouterIdGeneratedCase ROUTE_ID_CASE = new RouterIdGeneratedCaseBuilder()
43             .setRouterIdGenerated(new RouterIdGeneratedBuilder()
44                     .setLocalDiscriminator(LD).setRouterId(ROUTE_ID).build()).build();
45     private static final String ROUTE_ID_MODEL = "42.42.42.42";
46     private RouterIdParser parser;
47
48     @Before
49     public void setUp() {
50         this.parser = new RouterIdParser();
51     }
52
53     @Test
54     public void parserTest() {
55         final ByteBuf buff = Unpooled.buffer(VALUE_SIZE);
56
57         this.parser.serializeEsi(ROUTE_ID_CASE, buff);
58         assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
59
60         final Esi acResult = this.parser.parseEsi(Unpooled.wrappedBuffer(VALUE));
61         assertEquals(ROUTE_ID_CASE, acResult);
62
63         final Esi acmResult = this.parser.serializeEsi(RouterIdParserTest.createRouteContainer());
64         assertEquals(ROUTE_ID_CASE, acmResult);
65     }
66
67     public static ChoiceNode createRouterIdCase() {
68         return Builders.choiceBuilder()
69             .withNodeIdentifier(new NodeIdentifier(RouterIdGeneratedCase.QNAME))
70             .addChild(createRouteContainer())
71             .build();
72     }
73
74     private static ContainerNode createRouteContainer() {
75         return createContBuilder(new NodeIdentifier(RouterIdGenerated.QNAME))
76             .addChild(createValueBuilder(ROUTE_ID_MODEL, RD_NID).build())
77             .addChild(createValueBuilder(LD, LD_NID).build())
78             .build();
79     }
80
81     @Test(expected = IllegalArgumentException.class)
82     public void wrongCaseTest() {
83         this.parser.serializeEsi(new ArbitraryCaseBuilder().build(), null);
84     }
85 }