702e8e4cd432382e365623a0fd282987005dfcfe
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / MACIpAdvRParserTest.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.MAC;
17 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MAC_MODEL;
18 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL;
19 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL2;
20 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL2_MODEL;
21 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL_MODEL;
22 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VLAN;
23 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
24 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
25 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest.LAN_AUT_GEN_CASE;
26 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETI;
27 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ROUDE_DISTIN;
28 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.ETI_NID;
29 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.IP_NID;
30 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MAC_NID;
31 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS1_NID;
32 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS2_NID;
33 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.VLAN_NID;
34
35 import io.netty.buffer.Unpooled;
36 import java.util.ArrayList;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.ESIActivator;
40 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest;
41 import org.opendaylight.protocol.util.ByteArray;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.EvpnChoice;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.EsRouteCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.MacIpAdvRouteCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.mac.ip.adv.route.MacIpAdvRouteBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
52
53 public class MACIpAdvRParserTest {
54     private static final byte[] VALUE = {
55         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00,
56         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
57         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
58         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01,
59         (byte) 0x05, (byte) 0xdc, (byte) 0x10,
60         (byte) 0x05, (byte) 0xdc, (byte) 0x20
61     };
62     private static final byte[] RESULT = {
63         (byte) 0x02, (byte) 0x28,
64         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
65         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00,
66         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
67         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
68         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01,
69         (byte) 0x05, (byte) 0xdc, (byte) 0x10,
70         (byte) 0x05, (byte) 0xdc, (byte) 0x20
71     };
72     private static final byte[] VALUE2 = {
73         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00,
74         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
75         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
76         (byte) 0x80, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
77         (byte) 0x05, (byte) 0xdc, (byte) 0x10,
78     };
79     private static final byte[] RESULT2 = {
80         (byte) 0x02, (byte) 0x31,
81         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,  //RD
82         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00, //ESI
83         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, // ETI
84         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, // MAC
85         (byte) 0x80,//IPV6
86         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
87         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
88         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
89         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
90         (byte) 0x05, (byte) 0xdc, (byte) 0x10, //MPLS
91     };
92
93     private MACIpAdvRParser parser;
94
95     @Before
96     public void setUp() {
97         this.parser = new MACIpAdvRParser();
98         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
99     }
100
101     @Test
102     public void parserCase1Test() {
103         final MacIpAdvRouteCase expected = createdExpectedResult();
104         assertArrayEquals(RESULT, ByteArray.getAllBytes(this.parser.serializeEvpn(expected, Unpooled.wrappedBuffer(ROUDE_DISTIN))));
105
106         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
107         assertEquals(expected, result);
108         final EvpnChoice modelResult = this.parser.serializeEvpnModel(createMacIpCont());
109         assertEquals(expected, modelResult);
110
111         final EvpnChoice keyResult = this.parser.createRouteKey(createMacIpCont());
112         assertEquals(createdExpectedRouteKey(), keyResult);
113     }
114
115     static MacIpAdvRouteCase createdExpectedResult() {
116         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder().setEsi(LAN_AUT_GEN_CASE).setEthernetTagId(ETI)
117             .setMacAddress(MAC).setIpAddress(IP).setMplsLabel1(MPLS_LABEL).setMplsLabel2(MPLS_LABEL2).build()).build();
118     }
119
120     static MacIpAdvRouteCase createdExpectedRouteKey() {
121         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder().setEthernetTagId(ETI).setMacAddress(MAC)
122             .setIpAddress(IP).build()).build();
123     }
124
125     static ContainerNode createMacIpCont() {
126         return createContBuilder(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID)
127             .addChild(LanParserTest.createLanChoice())
128             .addChild(createEti())
129             .addChild(createValueBuilder(MAC_MODEL, MAC_NID).build())
130             .addChild(createValueBuilder(IP_MODEL, IP_NID).build())
131             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS1_NID).build())
132             .addChild(createValueBuilder(MPLS_LABEL2_MODEL, MPLS2_NID).build())
133             .build();
134     }
135
136     public static ContainerNode createEti() {
137         return createContBuilder(ETI_NID).withChild(createValueBuilder(VLAN, VLAN_NID).build()).build();
138     }
139
140     @Test
141     public void parserCase2Test() {
142
143         final MacIpAdvRouteCase expected = new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder().setEsi(LAN_AUT_GEN_CASE)
144             .setEthernetTagId(ETI).setMacAddress(MAC).setIpAddress(IPV6).setMplsLabel1(MPLS_LABEL).build()).build();
145         assertArrayEquals(RESULT2, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,  Unpooled.wrappedBuffer(ROUDE_DISTIN))));
146
147         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE2));
148         assertEquals(expected, result);
149
150         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
151         choice.withNodeIdentifier(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID);
152         final ContainerNode macIp = createContBuilder(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID).addChild(LanParserTest.createLanChoice()).addChild(createEti())
153             .addChild(createValueBuilder(MAC_MODEL, MAC_NID).build()).addChild(createValueBuilder(IPV6_MODEL, IP_NID).build())
154             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS1_NID).build()).build();
155         final EvpnChoice modelResult = this.parser.serializeEvpnModel(macIp);
156
157         assertEquals(expected, modelResult);
158     }
159
160     @Test(expected = IllegalArgumentException.class)
161     public void wrongCaseTest() {
162         this.parser.serializeEvpn(new EsRouteCaseBuilder().build(), null);
163     }
164 }