5a26a910f00ac922b1f8a3864c932003614f0cdb
[bgpcep.git] / bgp / 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.rev171213.es.route.EsRouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.EvpnChoice;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.EsRouteCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.EsRouteCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.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, (byte) 0x02, (byte) 0x00,
44         (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, (byte) 0x02, (byte) 0x00,
50         (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, (byte) 0x02, (byte) 0x00,
54         (byte) 0x80,//IPV6
55         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
56         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
57         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
58         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
59     };
60     private static final byte[] RESULT2 = {
61         (byte) 0x04, (byte) 0x23,
62         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
63         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00,
64         (byte) 0x80,//IPV6
65         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
66         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
67         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
68         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
69     };
70     private EthSegRParser parser;
71
72     @Before
73     public void setUp() {
74         this.parser = new EthSegRParser();
75         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
76     }
77
78     @Test
79     public void parserTest() {
80         final EsRouteCase expected = new EsRouteCaseBuilder().setEsRoute(new EsRouteBuilder().setEsi(LAN_AUT_GEN_CASE).setOrigRouteIp(IP).build()).build();
81         assertArrayEquals(RESULT, ByteArray.getAllBytes(this.parser.serializeEvpn(expected, Unpooled.wrappedBuffer(ROUDE_DISTIN))));
82
83         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
84         assertEquals(expected, result);
85
86         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
87         choice.withNodeIdentifier(EthSegRParser.ES_ROUTE_NID);
88         final ContainerNode arbitraryC = createContBuilder(EthSegRParser.ES_ROUTE_NID).addChild(LanParserTest.createLanChoice())
89             .addChild(createValueBuilder(IP_MODEL, ORI_NID).build()).build();
90         final EvpnChoice modelResult = this.parser.serializeEvpnModel(arbitraryC);
91         assertEquals(expected, modelResult);
92
93         final EvpnChoice keyResult = this.parser.createRouteKey(arbitraryC);
94         assertEquals(expected, keyResult);
95     }
96
97     @Test
98     public void parser2Test() {
99         final EsRouteCase expected = new EsRouteCaseBuilder().setEsRoute(new EsRouteBuilder().setEsi(LAN_AUT_GEN_CASE).setOrigRouteIp(IPV6).build()).build();
100         assertArrayEquals(RESULT2, ByteArray.getAllBytes(this.parser.serializeEvpn(expected, Unpooled.wrappedBuffer(ROUDE_DISTIN))));
101
102         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE2));
103         assertEquals(expected, result);
104
105         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
106         choice.withNodeIdentifier(EthSegRParser.ES_ROUTE_NID);
107         final ContainerNode arbitraryC = createContBuilder(EthSegRParser.ES_ROUTE_NID).addChild(LanParserTest.createLanChoice())
108             .addChild(createValueBuilder(IPV6_MODEL, ORI_NID).build()).build();
109         final EvpnChoice modelResult = this.parser.serializeEvpnModel(arbitraryC);
110         assertEquals(expected, modelResult);
111
112         final EvpnChoice keyResult = this.parser.createRouteKey(arbitraryC);
113         assertEquals(expected, keyResult);
114     }
115
116     @Test(expected = IllegalArgumentException.class)
117     public void wrongCaseTest() {
118         this.parser.serializeEvpn(new MacIpAdvRouteCaseBuilder().build(), null);
119     }
120
121     @Test(expected = IllegalArgumentException.class)
122     public void wrongSizeTest() {
123         this.parser.parseEvpn(Unpooled.wrappedBuffer(WRONG_VALUE));
124     }
125 }