c643ea4dd7ae81b43e0c6ed7a5aa8a215a20063c
[bgpcep.git] / bgp / 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.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.Esi;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.ArbitraryCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.RouterIdGeneratedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.RouterIdGeneratedCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.router.id.generated._case.RouterIdGenerated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.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 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
36
37 public class RouterIdParserTest {
38     public static final byte[] RESULT = {(byte) 0x04, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02, (byte) 0x02, (byte) 0x02,
39         (byte) 0x02, (byte) 0x00};
40     private static final byte[] VALUE = {(byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02, (byte) 0x02, (byte) 0x02,
41         (byte) 0x02, (byte) 0x00};
42     private static final Ipv4Address ROUTE_ID = new Ipv4Address("42.42.42.42");
43     public static final RouterIdGeneratedCase ROUTE_ID_CASE = new RouterIdGeneratedCaseBuilder().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         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> nextHop = Builders.choiceBuilder();
69         nextHop.withNodeIdentifier(new NodeIdentifier(RouterIdGeneratedCase.QNAME));
70         return nextHop.addChild(createRouteContainer()).build();
71     }
72
73     private static ContainerNode createRouteContainer() {
74         return createContBuilder(new NodeIdentifier(RouterIdGenerated.QNAME))
75             .addChild(createValueBuilder(ROUTE_ID_MODEL, RD_NID).build())
76             .addChild(createValueBuilder(LD, LD_NID).build())
77             .build();
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void wrongCaseTest() {
82         this.parser.serializeEsi(new ArbitraryCaseBuilder().build(), null);
83     }
84 }