Create common parent for extensions families
[bgpcep.git] / bgp / extensions / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EthSegRParserTest.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.IP;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IPV6;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IPV6_MODEL;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IP_MODEL;
16 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
17 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
18 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest.LAN_AUT_GEN_CASE;
19 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ROUDE_DISTIN;
20 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.WRONG_VALUE;
21 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.ORI_NID;
22
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.es.route.EsRouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EsRouteCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EsRouteCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
40
41 public class EthSegRParserTest {
42     private static final byte[] VALUE = {
43         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
44         (byte) 0x02, (byte) 0x00, (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01
45     };
46     private static final byte[] RESULT = {
47         (byte) 0x04, (byte) 0x17,
48         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
49         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
50         (byte) 0x02, (byte) 0x00, (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01
51     };
52     private static final byte[] VALUE2 = {
53         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
54         (byte) 0x02, (byte) 0x00,
55         (byte) 0x80,//IPV6
56         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
57         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
58         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
59         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
60     };
61     private static final byte[] RESULT2 = {
62         (byte) 0x04, (byte) 0x23,
63         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
64         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
65         (byte) 0x02, (byte) 0x00,
66         (byte) 0x80,//IPV6
67         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
68         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
69         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
70         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
71     };
72     private EthSegRParser parser;
73
74     @Before
75     public void setUp() {
76         this.parser = new EthSegRParser();
77         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
78     }
79
80     @Test
81     public void parserTest() {
82         final EsRouteCase expected = new EsRouteCaseBuilder().setEsRoute(new EsRouteBuilder()
83                 .setEsi(LAN_AUT_GEN_CASE).setOrigRouteIp(IP).build()).build();
84         assertArrayEquals(RESULT, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,
85                 Unpooled.wrappedBuffer(ROUDE_DISTIN))));
86
87         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
88         assertEquals(expected, result);
89
90         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choice = Builders
91                 .choiceBuilder();
92         choice.withNodeIdentifier(EthSegRParser.ES_ROUTE_NID);
93         final ContainerNode arbitraryC = createContBuilder(EthSegRParser.ES_ROUTE_NID)
94                 .addChild(LanParserTest.createLanChoice())
95                 .addChild(createValueBuilder(IP_MODEL, ORI_NID).build()).build();
96         final EvpnChoice modelResult = this.parser.serializeEvpnModel(arbitraryC);
97         assertEquals(expected, modelResult);
98
99         final EvpnChoice keyResult = this.parser.createRouteKey(arbitraryC);
100         assertEquals(expected, keyResult);
101     }
102
103     @Test
104     public void parser2Test() {
105         final EsRouteCase expected = new EsRouteCaseBuilder().setEsRoute(new EsRouteBuilder()
106                 .setEsi(LAN_AUT_GEN_CASE).setOrigRouteIp(IPV6).build()).build();
107         assertArrayEquals(RESULT2, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,
108                 Unpooled.wrappedBuffer(ROUDE_DISTIN))));
109
110         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE2));
111         assertEquals(expected, result);
112
113         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choice = Builders
114                 .choiceBuilder();
115         choice.withNodeIdentifier(EthSegRParser.ES_ROUTE_NID);
116         final ContainerNode arbitraryC = createContBuilder(EthSegRParser.ES_ROUTE_NID)
117                 .addChild(LanParserTest.createLanChoice())
118                 .addChild(createValueBuilder(IPV6_MODEL, ORI_NID).build()).build();
119         final EvpnChoice modelResult = this.parser.serializeEvpnModel(arbitraryC);
120         assertEquals(expected, modelResult);
121
122         final EvpnChoice keyResult = this.parser.createRouteKey(arbitraryC);
123         assertEquals(expected, keyResult);
124     }
125
126     @Test(expected = IllegalArgumentException.class)
127     public void wrongCaseTest() {
128         this.parser.serializeEvpn(new MacIpAdvRouteCaseBuilder().build(), null);
129     }
130
131     @Test(expected = IllegalArgumentException.class)
132     public void wrongSizeTest() {
133         this.parser.parseEvpn(Unpooled.wrappedBuffer(WRONG_VALUE));
134     }
135 }