cdce14ed5f3262c26d3b02df2ea6d5a810144275
[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.rev160321.ethernet.a.d.route.EthernetADRouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.ethernet.tag.id.EthernetTagId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.ethernet.tag.id.EthernetTagIdBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.evpn.choice.EsRouteCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.evpn.choice.EthernetADRouteCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.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, (byte) 0x02, (byte) 0x00,
48         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x05, (byte) 0xdc, (byte) 0x10
49     };
50     public static final byte[] ROUDE_DISTIN = {
51         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02
52     };
53     static final EthernetTagId ETI = new EthernetTagIdBuilder().setVlanId(VLAN).build();
54     private static final EthernetADRouteCase ETHERNET_AD_ROUTE_CASE_KEY = new EthernetADRouteCaseBuilder().setEthernetADRoute(new EthernetADRouteBuilder().setEsi(LAN_AUT_GEN_CASE).setEthernetTagId(ETI).build()).build();
55     public static final EthernetADRouteCase ETHERNET_AD_ROUTE_CASE = new EthernetADRouteCaseBuilder().setEthernetADRoute(new EthernetADRouteBuilder()
56         .setEsi(LAN_AUT_GEN_CASE).setEthernetTagId(ETI).setMplsLabel(MPLS_LABEL).build()).build();
57     static final byte[] WRONG_VALUE = {(byte) 0x00};
58     private static final byte[] VALUE = {
59         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00,
60         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x05, (byte) 0xdc, (byte) 0x10
61     };
62     private EthADRParser parser;
63
64     @Before
65     public void setUp() {
66         this.parser = new EthADRParser();
67         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
68     }
69
70     @Test
71     public void parserTest() {
72         final ByteBuf buff = this.parser.serializeEvpn(ETHERNET_AD_ROUTE_CASE, Unpooled.wrappedBuffer(ROUDE_DISTIN));
73         assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
74
75         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
76         assertEquals(ETHERNET_AD_ROUTE_CASE, result);
77
78         final EvpnChoice modelResult = this.parser.serializeEvpnModel(EthADRParserTest.createArbitraryContainer());
79         assertEquals(ETHERNET_AD_ROUTE_CASE, modelResult);
80
81         final EvpnChoice keyResult = this.parser.createRouteKey(EthADRParserTest.createArbitraryContainer());
82         assertEquals(ETHERNET_AD_ROUTE_CASE_KEY, keyResult);
83     }
84
85     public static ChoiceNode createEthADRModel() {
86         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
87         choice.withNodeIdentifier(EVPN_NID);
88         return choice.addChild(createArbitraryContainer()).build();
89     }
90
91     private static ContainerNode createArbitraryContainer() {
92         final ChoiceNode esiModel = LanParserTest.createLanChoice();
93         return createContBuilder(EthADRParser.ETH_AD_ROUTE_NID).addChild(esiModel).addChild(createEti())
94             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS_NID).build()).build();
95     }
96
97     @Test(expected = IllegalArgumentException.class)
98     public void wrongCaseTest() {
99         this.parser.serializeEvpn(new EsRouteCaseBuilder().build(), null);
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void wrongSizeTest() {
104         this.parser.parseEvpn(Unpooled.wrappedBuffer(WRONG_VALUE));
105     }
106 }