Code clean up
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EthADRParserTest.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.nlri;
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.MPLS_LABEL;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL_MODEL;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VLAN;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
16 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
17 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest.LAN_AUT_GEN_CASE;
18 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.MACIpAdvRParserTest.createEti;
19 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS_NID;
20 import static org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEvpnNlriRegistryTest.EVPN_NID;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.Unpooled;
24 import java.util.ArrayList;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.ESIActivator;
28 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.a.d.route.EthernetADRouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagIdBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EsRouteCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EthernetADRouteCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EthernetADRouteCaseBuilder;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
42
43 public class EthADRParserTest {
44     public static final byte[] RESULT = {
45         (byte) 0x01, (byte) 0x19,
46         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
47         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
48         (byte) 0x02, (byte) 0x00,
49         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x05, (byte) 0xdc, (byte) 0x10
50     };
51     public static final byte[] ROUDE_DISTIN = {
52         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02
53     };
54     static final EthernetTagId ETI = new EthernetTagIdBuilder().setVlanId(VLAN).build();
55     public static final EthernetADRouteCase ETHERNET_AD_ROUTE_CASE_KEY = new EthernetADRouteCaseBuilder()
56         .setEthernetADRoute(new EthernetADRouteBuilder().setEsi(LAN_AUT_GEN_CASE)
57                 .setEthernetTagId(ETI).build()).build();
58     public static final EthernetADRouteCase ETHERNET_AD_ROUTE_CASE = new EthernetADRouteCaseBuilder()
59             .setEthernetADRoute(new EthernetADRouteBuilder()
60         .setEsi(LAN_AUT_GEN_CASE).setEthernetTagId(ETI).setMplsLabel(MPLS_LABEL).build()).build();
61     static final byte[] WRONG_VALUE = {(byte) 0x00};
62     private static final byte[] VALUE = {
63         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
64         (byte) 0x02, (byte) 0x00,
65         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x05, (byte) 0xdc, (byte) 0x10
66     };
67     private EthADRParser parser;
68
69     @Before
70     public void setUp() {
71         this.parser = new EthADRParser();
72         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
73     }
74
75     @Test
76     public void parserTest() {
77         final ByteBuf buff = this.parser.serializeEvpn(ETHERNET_AD_ROUTE_CASE, Unpooled.wrappedBuffer(ROUDE_DISTIN));
78         assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
79
80         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
81         assertEquals(ETHERNET_AD_ROUTE_CASE, result);
82
83         final EvpnChoice modelResult = this.parser.serializeEvpnModel(EthADRParserTest.createArbitraryContainer());
84         assertEquals(ETHERNET_AD_ROUTE_CASE, modelResult);
85
86         final EvpnChoice keyResult = this.parser.createRouteKey(EthADRParserTest.createArbitraryContainer());
87         assertEquals(ETHERNET_AD_ROUTE_CASE_KEY, keyResult);
88     }
89
90     public static ChoiceNode createEthADRModel() {
91         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
92         choice.withNodeIdentifier(EVPN_NID);
93         return choice.addChild(createArbitraryContainer()).build();
94     }
95
96     private static ContainerNode createArbitraryContainer() {
97         final ChoiceNode esiModel = LanParserTest.createLanChoice();
98         return createContBuilder(EthADRParser.ETH_AD_ROUTE_NID).addChild(esiModel).addChild(createEti())
99             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS_NID).build()).build();
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void wrongCaseTest() {
104         this.parser.serializeEvpn(new EsRouteCaseBuilder().build(), null);
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void wrongSizeTest() {
109         this.parser.parseEvpn(Unpooled.wrappedBuffer(WRONG_VALUE));
110     }
111 }